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

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

Description

get the value for an attribute.

License

MIT License

Declaration

public static String getAttribute(Node node, String attribName) 

Method Source Code

//package com.java2s;
/** Copyright by Barry G. Becker, 2000-2011. Licensed under MIT License: http://www.opensource.org/licenses/MIT  */

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

public class Main {
    /**/*from   w  ww.  jav a  2 s . c o  m*/
     * get the value for an attribute.
     * Error if the attribute does not exist.
     */
    public static String getAttribute(Node node, String attribName) {

        String attributeVal = getAttribute(node, attribName, null);
        assert (attributeVal != null) : "no attribute named '" + attribName + "' for node '" + node.getNodeName()
                + "' val=" + node.getNodeValue();
        return attributeVal;
    }

    /**
    * get the value for an attribute. If not found, defaultValue is used.
    */
    public static String getAttribute(Node node, String attribName, String defaultValue) {
        NamedNodeMap attribMap = node.getAttributes();
        String attributeVal = null;
        if (attribMap == null) {
            return null;
        }

        for (int i = 0; i < attribMap.getLength(); i++) {
            Node attr = attribMap.item(i);
            if (attr.getNodeName().equals(attribName))
                attributeVal = attr.getNodeValue();
        }
        if (attributeVal == null)
            attributeVal = defaultValue;

        return attributeVal;
    }
}

Related

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