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

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

Description

get Attribute

License

LGPL

Declaration

public static String getAttribute(final Node node, final String attributeName) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

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

public class Main {
    public static String getAttribute(final Node node, final String attributeName) {
        final NamedNodeMap tmpMap = node.getAttributes();
        if (tmpMap == null) {
            return null;
        }/*from  w w  w .j  a v a 2 s.c o  m*/
        final Node tmpNode = tmpMap.getNamedItem(attributeName);
        if (tmpNode == null) {
            return null;
        }
        return tmpNode.getNodeValue();
    }

    public static String[] getAttributes(final Node node, final String[] attributeNames) {
        final String[] valueList = new String[attributeNames.length];
        final NamedNodeMap attMap = node.getAttributes();
        Node tmpNode = null;
        for (int i = 0; i < attributeNames.length; i++) {
            try {
                tmpNode = attMap.getNamedItem(attributeNames[i]);
                valueList[i] = tmpNode.getNodeValue();
            } catch (Exception e) {
                valueList[i] = "";
            }
        } // next attribute
        return valueList;
    }
}

Related

  1. getAttribute(File file, String attr)
  2. getAttribute(final List elements, final int index)
  3. getAttribute(final Node iNode, final String iAttributeName)
  4. getAttribute(final Node n, final String attrName, final String defaultValue)
  5. getAttribute(final Node node, final String attribname, final int def)
  6. getAttribute(final Node node, final String name)
  7. getAttribute(final Node xml, final String namespaceURI, final String localName)
  8. getAttribute(NamedNodeMap map, String name)
  9. getAttribute(NamedNodeMap namedNodeMap, String name)