Java 怎么获取指定XML属性的值作为布尔值

2019年7月21日19:51:34Java 怎么获取指定XML属性的值作为布尔值已关闭评论 355 937字阅读3分7秒

1.简介

Java 怎么获取指定XML属性的值作为布尔值?——请看下文。

2.示例代码

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


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

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

        if (v != null) {
            try {
                result = "true".equalsIgnoreCase(v)
                        || "yes".equalsIgnoreCase(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;
        }
    }
}

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