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

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

Description

Returns the attribute value for the passed name in the passed node.

License

Open Source License

Parameter

Parameter Description
node the node to get the attribute from
name the name of the attribute
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.

Declaration

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

Method Source Code

//package com.java2s;
/**// w  w w. j av a2s  .  co  m
 * Helios, OpenSource Monitoring
 * Brought to you by the Helios Development Group
 *
 * Copyright 2007, Helios Development Group and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org. 
 *
 */

import org.w3c.dom.Attr;

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

public class Main {
    /**
     * 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. getAttributeByIndex(final Element element, final int index)
  2. getAttributeByLocalName(XMLStreamReader reader, String localName)
  3. getAttributeByName(final List elements, final String attributeName)
  4. getAttributeByName(Node content, String attributeName)
  5. getAttributeByName(Node node, String name, boolean defaultValue)
  6. getAttributeContent(Node item, String attributeName)
  7. getAttributeEnum(Node node, String attributeName, Class enumClass)
  8. getAttributeFloatValue(String attribute, NamedNodeMap namedNodeMap)
  9. getAttributeFromClosestAncestorOfAnyKind(Node node, String attributeName)