Java XML Attribute Get getAttributeByName(Node node, String name, boolean defaultValue)

Here you can find the source of getAttributeByName(Node node, String name, boolean defaultValue)

Description

Returns the value of a named node attribute in the form of a boolean

License

LGPL

Parameter

Parameter Description
node The node to retrieve the attribute from
name The name of the attribute
defaultValue The default value if the attribute cannot be located or converted.

Return

true or false

Declaration

public static boolean getAttributeByName(Node node, String name, boolean defaultValue) 

Method Source Code


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

import org.w3c.dom.*;

public class Main {
    /**// w w w  . ja v a  2s . c o m
     * Returns the attribute value for the passed name in the passed node.
     * @param node the node to get the attribute from
     * @param name the name of the attribute
     * @param defaultValue the value to return if the node did not contain the attribute
     * @return The attribute value or the default value if it is not found.
     */
    public static String getAttributeByName(Node node, String name, String defaultValue) {
        try {
            String val = getAttributeValueByName(node, name);
            if (val != null && !val.trim().isEmpty())
                return val.trim();
        } catch (Exception e) {
        }
        return defaultValue;
    }

    /**
     * Returns the value of a named node attribute in the form of a boolean
    * @param node The node to retrieve the attribute from
    * @param name The name of the attribute
    * @param defaultValue The default value if the attribute cannot be located or converted.
    * @return true or false
    */
    public static boolean getAttributeByName(Node node, String name, boolean defaultValue) {
        if (node == null || name == null)
            return defaultValue;
        try {
            return getAttributeBooleanByName(node.getAttributes(), name);
        } catch (Exception e) {
            return defaultValue;
        }
    }

    /**
     * Returns the value of a named node attribute in the form of an int
    * @param node The node to retrieve the attribute from
    * @param name The name of the attribute
    * @param defaultValue The default value if the attribute cannot be located or converted.
    * @return an int
    */
    public static int getAttributeByName(Node node, String name, int defaultValue) {
        if (node == null || name == null)
            return defaultValue;
        try {
            return new Double(getAttributeValueByName(node, name).trim()).intValue();
        } catch (Exception e) {
            return defaultValue;
        }
    }

    /**
     * Returns the value of a named node attribute in the form of a long
    * @param node The node to retrieve the attribute from
    * @param name The name of the attribute
    * @param defaultValue The default value if the attribute cannot be located or converted.
    * @return a long
    */
    public static long getAttributeByName(Node node, String name, long defaultValue) {
        if (node == null || name == null)
            return defaultValue;
        try {
            return new Double(getAttributeValueByName(node, name).trim()).longValue();
        } catch (Exception e) {
            return defaultValue;
        }
    }

    /**
     * Searches throgh the passed NamedNodeMap for an attribute and returns it if it is found.
     * If it is not found, returns a null.
     * @param nnm NamedNodeMap
     * @param name String
     * @return String
     */
    public static String getAttributeValueByName(NamedNodeMap nnm, String name) {
        for (int i = 0; i < nnm.getLength(); i++) {
            Attr attr = (Attr) nnm.item(i);
            if (attr.getName().equalsIgnoreCase(name)) {
                return attr.getValue();
            }
        }
        return null;
    }

    /**
     * Returns the attribute value for the passed name in the passed node.
     * @param node the node to get the attribute from
     * @param name the name of the attribute
     * @return The attribute value or null if it is not found.
     */
    public static String getAttributeValueByName(Node node, String name) {
        return getAttributeValueByName(node.getAttributes(), name);
    }

    /**
     * Searches throgh the passed NamedNodeMap for an attribute. If it is found, it will try to convert it to a boolean.
     * @param nnm NamedNodeMap
     * @param name String
     * @throws RuntimeException on any failure to parse a boolean
     * @return boolean
     */
    public static boolean getAttributeBooleanByName(NamedNodeMap nnm, String name) throws RuntimeException {
        for (int i = 0; i < nnm.getLength(); i++) {
            Attr attr = (Attr) nnm.item(i);
            if (attr.getName().equalsIgnoreCase(name)) {
                String tmp = attr.getValue().toLowerCase();
                if (tmp.equalsIgnoreCase("true"))
                    return true;
                if (tmp.equalsIgnoreCase("false"))
                    return false;
                throw new RuntimeException("Attribute " + name + " value not boolean:" + tmp);
            }
        }
        throw new RuntimeException("Attribute " + name + " not found.");
    }
}

Related

  1. getAttributeBooleanByName(NamedNodeMap nnm, String name)
  2. getAttributeByIndex(final Element element, final int index)
  3. getAttributeByLocalName(XMLStreamReader reader, String localName)
  4. getAttributeByName(final List elements, final String attributeName)
  5. getAttributeByName(Node content, String attributeName)
  6. getAttributeByName(Node node, String name, String defaultValue)
  7. getAttributeContent(Node item, String attributeName)
  8. getAttributeEnum(Node node, String attributeName, Class enumClass)
  9. getAttributeFloatValue(String attribute, NamedNodeMap namedNodeMap)