Java XML Attribute Get getAttributeValue(Node node, String attribute)

Here you can find the source of getAttributeValue(Node node, String attribute)

Description

Returns an attribute value of a specified node.

License

Creative Commons License

Parameter

Parameter Description
node The node containing the attribute.
attribute The name of the attribute.

Return

The attribute value.

Declaration

public static String getAttributeValue(Node node, String attribute) 

Method Source Code


//package com.java2s;
//License from project: Creative Commons License 

import org.w3c.dom.Node;

public class Main {
    /**/*from  w ww  . ja v  a2  s.  com*/
     * Returns an attribute value of a specified node. If this attribute does
     * not exits an empty string will be returned.
     * 
     * @param node The node containing the attribute.
     * @param attribute The name of the attribute.
     * @return The attribute value.
     */
    public static String getAttributeValue(Node node, String attribute) {
        return getAttributeValue(node, attribute, "");
    }

    /**
     * Returns an attribute value of a specified node. If this attribute does
     * not exits a specified default value will be returned.
     * 
     * @param node The node containing the attribute.
     * @param attribute The name of the attribute.
     * @param defaultValue The value returned if the attribute does not exist.
     * @return The attribute value.
     */
    public static String getAttributeValue(Node node, String attribute, String defaultValue) {
        try {
            return node.getAttributes().getNamedItem(attribute).getTextContent();
        } catch (Exception e) {
            return defaultValue;
        }
    }

    /**
     * Returns the text content of a node or an empty string if an error occurs.
     * 
     * @param node The node.
     * @return The text content or an empty string.
     */
    public static String getTextContent(Node node) {
        return getTextContent(node, "");
    }

    /**
     * Returns the text content of a node or an empty string if an error occurs.
     * 
     * @param node The node.
     * @param defaultValue The default value.
     * @return The text content or the default value.
     */
    public static String getTextContent(Node node, String defaultValue) {
        try {
            String content = null;
            if (node != null) {
                content = node.getTextContent();
            }
            return content != null ? content : defaultValue;
        } catch (Exception e) {
            return defaultValue;
        }
    }
}

Related

  1. getAttributeValue(Node n, String name)
  2. getAttributeValue(Node n, String name)
  3. getAttributeValue(Node n, String nodePath, String attrName)
  4. getAttributeValue(Node node, String attName)
  5. getAttributeValue(Node node, String attName)
  6. getAttributeValue(Node node, String attribute)
  7. getAttributeValue(Node node, String attribute)
  8. getAttributeValue(Node node, String attributeName)
  9. getAttributeValue(Node node, String attributeName)