Java XML Attribute Get getBooleanAttributeOptional(Node node, String attributeName, Boolean valueIfEmpty)

Here you can find the source of getBooleanAttributeOptional(Node node, String attributeName, Boolean valueIfEmpty)

Description

get Boolean Attribute Optional

License

Apache License

Declaration

public static Boolean getBooleanAttributeOptional(Node node, String attributeName, Boolean valueIfEmpty)
            throws Exception 

Method Source Code

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

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

public class Main {
    private static final String VALUE_TRUE = "true";
    private static final String VALUE_FALSE = "false";

    public static Boolean getBooleanAttributeOptional(Node node, String attributeName, Boolean valueIfEmpty)
            throws Exception {
        String string = getStringAttributeOptional(node, attributeName, null);
        if (string == null) {
            return valueIfEmpty;
        }/*from w ww  .  j  av  a2  s.c o  m*/
        if (VALUE_TRUE.equals(string)) {
            return true;
        } else if (VALUE_FALSE.equals(string)) {
            return false;
        }
        throw new Exception("Could not read boolean from value '" + string + "' in attribute '" + attributeName
                + "' in node '" + node.getLocalName() + "'");
    }

    public static String getStringAttributeOptional(Node node, String attributeName, String valueIfEmpty) {
        NamedNodeMap attributes = node.getAttributes();
        Node value = attributes.getNamedItem(attributeName);
        if (value == null) {
            return valueIfEmpty;
        }
        String text = value.getTextContent();
        if (text == null) {
            return valueIfEmpty;
        }
        return text;
    }
}

Related

  1. getBooleanAttribute(Element element, String name, boolean defaultValue)
  2. getBooleanAttribute(Node n, String attributeName)
  3. getBooleanAttribute(Node targetElem, String keyName, boolean defaultValue)
  4. getBooleanAttributeByName(Node content, String attributeName, boolean defaultTrue)
  5. getBooleanAttributeOption(final Element configuration, final String option, boolean defaultValue)
  6. getBooleanAttributeRequired(Node node, String attributeName)
  7. getBooleanAttributeValue(Node node, String attribute)
  8. getCascadeValue(final Element elem, final String attrName)
  9. getClosestAncestorWithAttribute(Node node, String ancestorName, String attributeName)