Java XML Attribute Get getAttributeValueBoolean(Node node, String attrName)

Here you can find the source of getAttributeValueBoolean(Node node, String attrName)

Description

Returns boolean value of the named attribute of the node.

License

Open Source License

Parameter

Parameter Description
node The node to extract attribute from
attrName The name of the attribute

Exception

Parameter Description
NullPointerException If <code>node</code> is <code>null</code>

Return

Boolean value of the attribute or false if such attribute does not exist

Declaration

public static boolean getAttributeValueBoolean(Node node, String attrName) 

Method Source Code


//package com.java2s;
/*//w w w. j  a v a 2 s .c o  m
 * Copyright (C) 2016 Alexander Vasilevsky
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import org.w3c.dom.Node;

public class Main {
    /**
     * Returns boolean value of the named attribute of the node.
     * Returns <code>true</code> if attribute value equals "true"
     * ignore case or "1". Otherwise, returns <code>false</code>.
     *
     * @param node     The node to extract attribute from
     * @param attrName The name of the attribute
     *
     * @return Boolean value of the attribute or <code>false</code>
     *         if such attribute does not exist
     *
     * @throws NullPointerException If <code>node</code> is <code>null</code>
     */
    public static boolean getAttributeValueBoolean(Node node, String attrName) {
        String value = getAttributeValue(node, attrName);
        return "true".equalsIgnoreCase(value) || "1".equals(value);
    }

    /**
     * Returns string value of the named attribute of the node.
     * Returns <code>null</code> if such attribute does not exist.
     *
     * @param node     The node to extract attribute from
     * @param attrName The name of the attribute
     *
     * @return String value of the attribute or <code>null</code>
     *
     * @throws NullPointerException If <code>node</code> is <code>null</code>
     */
    public static String getAttributeValue(Node node, String attrName) {
        Node attr = node.getAttributes().getNamedItem(attrName);
        return attr == null ? null : attr.getNodeValue();
    }
}

Related

  1. getAttributeValueAsBoolean(Element el, String attrName)
  2. getAttributeValueAsDouble(Node node, String attributeName)
  3. getAttributeValueAsInteger(Node node, String attributeName, Integer defaultValue)
  4. getAttributeValueAsLong(final Element el, final String attrName, final long defaultVal)
  5. getAttributeValueAsString(Node node)
  6. getAttributeValueByName(NamedNodeMap nnm, String name)
  7. getAttributeValueByName(NamedNodeMap nnm, String name)
  8. getAttributeValueByName(Node node, String name)
  9. getAttributeValueByName(Node node, String name)