Java XML Attribute Get getBooleanAttributeRequired(Node node, String attributeName)

Here you can find the source of getBooleanAttributeRequired(Node node, String attributeName)

Description

get Boolean Attribute Required

License

Apache License

Declaration

public static boolean getBooleanAttributeRequired(Node node, String attributeName) 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 getBooleanAttributeRequired(Node node, String attributeName) throws Exception {
        String string = getStringAttributeRequired(node, attributeName);
        if (VALUE_TRUE.equals(string)) {
            return true;
        } else if (VALUE_FALSE.equals(string)) {
            return false;
        }/*from w w w  .ja  v a 2s  . c  o  m*/
        throw new Exception("Could not read boolean from value '" + string + "' in attribute '" + attributeName
                + "' in node '" + node.getLocalName() + "'");
    }

    public static String getStringAttributeRequired(Node node, String attributeName) throws Exception {
        NamedNodeMap attributes = node.getAttributes();
        Node value = attributes.getNamedItem(attributeName);
        if (value == null) {
            throw new Exception("Missing attribute '" + attributeName + "' in node '" + node.getNodeName() + "'");
        }
        String text = value.getTextContent();
        if (text == null) {
            throw new Exception("Missing text  '" + attributeName + "' in node '" + node.getNodeName() + "'");
        }

        return text;
    }
}

Related

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