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

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

Description

Get the value of a node's attribute.

License

Open Source License

Parameter

Parameter Description
node the node.
name the attribute.

Return

the value of the attribute, or an empty string if the node is not an element or the attribute is missing.

Declaration

public static String getAttributeValue(Node node, String name) 

Method Source Code

//package com.java2s;
/*---------------------------------------------------------------
*  Copyright 2005 by the Radiological Society of North America
*
*  This source software is released under the terms of the
*  RSNA Public License (http://mirc.rsna.org/rsnapubliclicense)
*----------------------------------------------------------------*/

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class Main {
    /**/*ww w  . j  ava 2s  . c  o m*/
     * Get the value of a node's attribute. If the node is a Document, use the
     * document element as the element node.
     * @param node the node.
     * @param name the attribute.
     * @return the value of the attribute, or an empty string if the
     * node is not an element or the attribute is missing.
     */
    public static String getAttributeValue(Node node, String name) {
        if (node instanceof Document)
            node = ((Document) node).getDocumentElement();
        if (!(node instanceof Element))
            return "";
        return ((Element) node).getAttribute(name);
    }
}

Related

  1. getAttributeValue(Node node, String attributeName, int defaultValue)
  2. getAttributeValue(Node node, String attrName)
  3. getAttributeValue(Node node, String attrName)
  4. getAttributeValue(Node node, String name)
  5. getAttributeValue(Node node, String name)
  6. getAttributeValue(Node node, String name, boolean defaultValue)
  7. getAttributeValue(Node node, String name, String defaultValue)
  8. getAttributeValue(Node sNode, String attribName)
  9. getAttributeValue(Node sNode, String attribName)