Java XML Attribute Get getAttributeAsBoolean(NamedNodeMap map, String name)

Here you can find the source of getAttributeAsBoolean(NamedNodeMap map, String name)

Description

Get a named value from the NamedNodeMap as a boolean.

License

Open Source License

Parameter

Parameter Description
map the NamedNodeMap to get a value from.
name the name of the attribute to find.

Return

value of the named attribute from the map as a boolean.

Declaration

static boolean getAttributeAsBoolean(NamedNodeMap map, String name) 

Method Source Code

//package com.java2s;
/*/*w  w w . j a va 2 s .c om*/
 * Knicker is Copyright 2010-2012 by Jeremy Brooks
 *
 * This file is part of Knicker.
 *
 * Knicker 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.
 *
 * Knicker 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 Knicker.  If not, see <http://www.gnu.org/licenses/>.
 */

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

public class Main {
    /**
     * Get a named value from the NamedNodeMap as a boolean.
     * <p/>
     * Returns true only if the attribute is "1".
     * <p/>
     * If the value does not exist, or if there is an error getting data from
     * the map, false will be returned.
     *
     * @param map  the NamedNodeMap to get a value from.
     * @param name the name of the attribute to find.
     * @return value of the named attribute from the map as a boolean.
     */
    static boolean getAttributeAsBoolean(NamedNodeMap map, String name) {
        boolean value = false;

        try {
            value = (getAttribute(map, name)).equals("1");
        } catch (Exception e) {
            // will return false
        }

        return value;
    }

    /**
     * Get a named value from the NamedNodeMap.
     * <p/>
     * If the value does not exist, or if there is an error getting data from
     * the map, an empty string will be returned.
     *
     * @param map  the NamedNodeMap to get a value from.
     * @param name the name of the attribute to find.
     * @return value of the named attribute from the map, or an empty String.
     */
    static String getAttribute(NamedNodeMap map, String name) {
        String value = "";

        try {
            Node node = map.getNamedItem(name);
            if (node != null) {
                value = node.getNodeValue().trim();
            }
        } catch (Exception e) {
            // ignore; will return empty string
        }

        return value;
    }
}

Related

  1. getAttribute(String name, Node node)
  2. getAttributeAsBoolean(Element elem, String attribName)
  3. getAttributeAsBoolean(Element element, String attrName, boolean defValue)
  4. getAttributeAsBoolean(Element element, String attrName, boolean defValue)
  5. getAttributeAsBoolean(Element element, String name)
  6. getAttributeAsInteger(Element element, String attrName, Integer defValue)
  7. getAttributeAsLong(Element element, String attrName, Long defValue)
  8. getAttributeAsString(NamedNodeMap attributes, String name)
  9. getAttributeAsString(XMLStreamReader reader, String name)