Java 怎么以double形式获取指定XML属性的值

2019年7月21日19:51:30Java 怎么以double形式获取指定XML属性的值已关闭评论 498 905字阅读3分1秒

1.简介

Java 怎么以double形式获取指定XML属性的值?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */


import org.w3c.dom.DOMException;
import org.w3c.dom.NamedNodeMap;

public class Main {
    
    public static double getAttributeValueAsDouble(NamedNodeMap attribs,
            String attributeName, double defValue) throws DOMException {
        String v = getAttributeValue(attribs, attributeName);
        double result = defValue;

        if (v != null) {
            try {
                result = Double.parseDouble(v);
            } catch (NumberFormatException ex) {
            }
        }
        return result;
    }

    
    public static String getAttributeValue(NamedNodeMap attribs,
            String attributeName) throws DOMException {
        String value = null;
        if (attribs.getNamedItem(attributeName) != null) {
            value = attribs.getNamedItem(attributeName).getNodeValue();
        }
        return value;
    }

    
    public static String getAttributeValue(NamedNodeMap attribs,
            String attributeName, String defValue) throws DOMException {
        if (attribs.getNamedItem(attributeName) != null) {
            return attribs.getNamedItem(attributeName).getNodeValue();
        } else {
            return defValue;
        }
    }
}

 
继续阅读
编程十万个怎么办