Java XML Attribute Get getAttributeValueAsBoolean(Element el, String attrName)

Here you can find the source of getAttributeValueAsBoolean(Element el, String attrName)

Description

Get the boolean value from the given attribute.

License

Apache License

Declaration

public static boolean getAttributeValueAsBoolean(Element el, String attrName) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import javax.xml.namespace.QName;

import org.w3c.dom.Element;

public class Main {
    /** Get the boolean value from the given attribute. */
    public static boolean getAttributeValueAsBoolean(Element el, String attrName) {
        return getAttributeValueAsBoolean(el, new QName(attrName));
    }// w  ww . jav a 2 s  .  c  o  m

    /** Get the boolean value from the given attribute. */
    public static boolean getAttributeValueAsBoolean(Element el, QName attrName) {
        String attrVal = getAttributeValue(el, attrName);
        boolean ret = "true".equalsIgnoreCase(attrVal) || "1".equalsIgnoreCase(attrVal);
        return ret;
    }

    /** 
     * Get the value from the given attribute
     * @return null if the attribute value is empty or the attribute is not present
     */
    public static String getAttributeValue(Element el, String attrName) {
        return getAttributeValue(el, new QName(attrName));
    }

    /** 
     * Get the value from the given attribute
     * @return null if the attribute value is empty or the attribute is not present
     */
    public static String getAttributeValue(Element el, QName attrName) {
        String attr = null;
        if ("".equals(attrName.getNamespaceURI()))
            attr = el.getAttribute(attrName.getLocalPart());
        else
            attr = el.getAttributeNS(attrName.getNamespaceURI(), attrName.getLocalPart());

        if ("".equals(attr))
            attr = null;

        return attr;
    }
}

Related

  1. getAttributeValue(NodeList elements, String attributeName)
  2. getAttributeValue(StartElement element, String namespaceURI, String localPart)
  3. getAttributeValue(StartElement startElement, String attributeName)
  4. getAttributeValue(String attributeName, Node xmlNode)
  5. getAttributeValueAsBoolean(Attr attribute)
  6. getAttributeValueAsDouble(Node node, String attributeName)
  7. getAttributeValueAsInteger(Node node, String attributeName, Integer defaultValue)
  8. getAttributeValueAsLong(final Element el, final String attrName, final long defaultVal)
  9. getAttributeValueAsString(Node node)