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

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

Description

This method will get the value of an attribute for a given node

License

BSD License

Parameter

Parameter Description
node the XML node which contain the attribute
name the name of the attrbiute

Return

String the string value of the attribute.

Declaration


public static String getAttribute(Node node, String name) 

Method Source Code

//package com.java2s;
/*L//from w w w.  j  av a 2  s.  c  o  m
 * Copyright SAIC, SAIC-Frederick.
 *
 * Distributed under the OSI-approved BSD 3-Clause License.
 * See http://ncip.github.com/caadapter/LICENSE.txt for details.
 */

import org.w3c.dom.Attr;

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

public class Main {
    /**
     * This method will get the value of an attribute for a given node
     *
     * @param node the XML node which contain the attribute
     * @param name the name of the attrbiute
     * @return String the string value of the attribute.
     */

    public static String getAttribute(Node node, String name) {
        NamedNodeMap attrs = node.getAttributes();
        if (attrs == null)
            return null;
        Attr attr = (Attr) attrs.getNamedItem(name);
        if (attr == null)
            return null;
        else
            return attr.getNodeValue();
    }
}

Related

  1. getAttribute(Node node, String localName, String namespaceURI)
  2. getAttribute(Node node, String name)
  3. getAttribute(Node node, String name)
  4. getAttribute(Node node, String name)
  5. getAttribute(Node node, String name)
  6. getAttribute(Node node, String name)
  7. getAttribute(Node node, String name)
  8. getAttribute(Node node, String name)
  9. getAttribute(Node node, String name)