Java XML Attribute Get getAttribute(Node node, String attributeName)

Here you can find the source of getAttribute(Node node, String attributeName)

Description

A safe way of getting attribute value of attribute with given name.

License

Open Source License

Parameter

Parameter Description
node a parameter
attributeName a parameter

Declaration

public static String getAttribute(Node node, String attributeName) 

Method Source Code

//package com.java2s;

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

public class Main {
    /**//from   w  w w. ja va 2  s.c  om
     * A safe way of getting attribute value of attribute with given name. If attribute with given name
     * doesn't exist, returns null
     * (instead of NPE).
     * 
     * @param node
     * @param attributeName
     * @return
     */
    public static String getAttribute(Node node, String attributeName) {
        if (node == null)
            return null;

        NamedNodeMap attributes = node.getAttributes();
        if (attributes == null)
            return null;

        Node attribute = attributes.getNamedItem(attributeName);
        if (attribute == null)
            return null;

        return attribute.getTextContent();
    }
}

Related

  1. getAttribute(Node node, String attr)
  2. getAttribute(Node node, String attr)
  3. getAttribute(Node node, String attr)
  4. getAttribute(Node node, String attribName)
  5. getAttribute(Node node, String attribute)
  6. getAttribute(Node node, String attributeName)
  7. getAttribute(Node node, String attributeName)
  8. getAttribute(Node node, String attributeName)
  9. getAttribute(Node node, String attributeName)