Java 怎么以整数(integer)形式获取指定XML属性的值

2019年7月21日19:51:27Java 怎么以整数(integer)形式获取指定XML属性的值已关闭评论 454 896字阅读2分59秒

1.简介

Java 怎么以整数(integer)形式获取指定XML属性的值?——请看下文。

2.示例代码

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


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

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

        if (v != null) {
            try {
                result = Integer.parseInt(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;
        }
    }
}

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