Java XML Attribute Get getAttributeString(Node node, String name)

Here you can find the source of getAttributeString(Node node, String name)

Description

Gets the value of a named attribute of a node.

License

CDDL license

Parameter

Parameter Description
node Node The context node.
name String

Return

String null means node is not found.

Declaration

public static String getAttributeString(Node node, String name) 

Method Source Code

//package com.java2s;
/*// ww  w  .j  av a 2s.co  m
Copyright (c) 2013 eBay, Inc.
This program is licensed under the terms of the eBay Common Development and
Distribution License (CDDL) Version 1.0 (the "License") and any subsequent  version 
thereof released by eBay.  The then-current version of the License can be found 
at http://www.opensource.org/licenses/cddl1.php and in the eBaySDKLicense file that 
is under the eBay SDK ../docs directory.
*/

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

public class Main {
    /**
     * Gets the value of a named attribute of a node.
     * @param node Node The context node.
     * @param name String
     * @return String null means node is not found.
     */
    public static String getAttributeString(Node node, String name) {
        Node nd = findAttribute(node, name);
        if (nd != null)
            return nd.getNodeValue();
        return null;
    }

    /**
     * Finds attribute of node by name.
     * @param node Node The context node.
     * @param name String
     * @return Node
     */
    public static Node findAttribute(Node node, String name) {
        Node found = null;
        NamedNodeMap nm = node.getAttributes();
        if (nm != null) {
            for (int i = 0; i < nm.getLength(); i++) {
                Node attr = nm.item(i);
                if (attr.getNodeName().compareToIgnoreCase(name) == 0) {
                    found = attr;
                    break;
                }
            }
        }
        return found;
    }
}

Related

  1. getAttributes(XMLEvent evt)
  2. getAttributesAsMap(Element e)
  3. getAttributesByName(Node node, ArrayList attrNames)
  4. getAttributesCompact(NamedNodeMap attributes)
  5. getAttributesNamesOf(Element element)
  6. getAttributeStringValue(Node htmlForm, String attributeName, String defaultValue)
  7. getAttributeStringValue(String attribute, NamedNodeMap namedNodeMap)
  8. getAttributesValues(final StartElement element)
  9. getAttributeTable(Element element)