Java XML Attribute from Node getNodeAttribute(Node thisNode, String key)

Here you can find the source of getNodeAttribute(Node thisNode, String key)

Description

Utility method to look for any key=value attribute for a Node and returns null if the attribute does not exist or contains the empty string.

License

Open Source License

Parameter

Parameter Description
thisNode the Node for which to find desired attribute

Return

String corresponding to the key= attribute, trimmed, or null if there is not a valid name.

Declaration

public static String getNodeAttribute(Node thisNode, String key) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    /**/* ww w  . j  a  v a 2 s.c o m*/
     * Utility method to look for any key=value attribute for a Node and returns
     * null if the attribute does not exist or contains the empty string.
     * 
     * @param thisNode  the Node for which to find desired attribute
     * @return String corresponding to the <code>key</code>= attribute,
     *         trimmed, or null if there is not a valid name.
     */
    public static String getNodeAttribute(Node thisNode, String key) {
        if (null == thisNode) {
            return null;
        }
        NamedNodeMap attributes = thisNode.getAttributes();
        if (null == attributes) {
            return null;
        }
        Node node = attributes.getNamedItem(key);
        if (null == node) {
            return null;
        }
        String value = node.getNodeValue();
        if (null == value || (null != value && value.trim().length() == 0)) {
            return null;
        }

        assert value != null; // value NOT null at this point
        return value.trim();
    }
}

Related

  1. getNodeAttribute(Node node, String name)
  2. getNodeAttribute(Node node, String name)
  3. getNodeAttribute(Node node, String name, String def)
  4. getNodeAttribute(Node node, String s)
  5. getNodeAttribute(Node QueryNode, String AttrName)
  6. getNodeAttributeAsInt(Node node, String attributeName, int defaultValue)
  7. getNodeAttributeDeep(NodeList nodelList, String nodeName, String nodeAttr)
  8. getNodeAttributes(final Node node)
  9. getNodeAttributesToString(Node node)