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

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

Description

Returns the value of the given attribute.

License

Open Source License

Parameter

Parameter Description
node The current node
attrName The attribute name

Return

The attribute's value

Declaration

public static String getAttribute(Node node, String attrName) 

Method Source Code

//package com.java2s;
/*/*from w w  w.  j a  va 2s  .  co m*/
 * XMLUtility.java  2/6/13 1:04 PM
 *
 * Copyright (C) 2012-2013 Nick Ma
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**
     * Returns the value of the given attribute.
     *
     * @param node     The current node
     * @param attrName The attribute name
     * @return The attribute's value
     */
    public static String getAttribute(Node node, String attrName) {
        // handle null node
        if (node == null) {
            return null;
        }
        Node attr = node.getAttributes().getNamedItem(attrName);
        if (attr != null) {
            return attr.getNodeValue();
        }
        return null;
    }

    /**
     * Searches parent node for matching child nodes, then collects and returns
     * the first match node's value. Useful when caller knows that there is only
     * one child node.
     * @param node     The parent node
     * @return The child node's value
     */
    public static String getNodeValue(Node node) {
        // sometimes jdom use more than one children nodes to represent text node 
        NodeList children = node.getChildNodes();
        String value = "";
        for (int i = 0; i < children.getLength(); i++) {
            value += children.item(i).getNodeValue();
        }
        return value;
    }

    /**
     * Searches parent node for matching child nodes, then collects and returns
     * the first match node's value. Useful when caller knows that there is only
     * one child node.
     *
     * @param node     The parent node
     * @param nodeName The child node element name
     * @return The child node's value
     */
    public static String getNodeValue(Node node, String nodeName) {
        Node n = getNode(node, nodeName);
        if (n == null) {
            return null;
        }
        Node ch = n.getFirstChild();
        if (ch != null) {
            return ch.getNodeValue();
        }
        return null;

    }

    /**
     * Searches parent node and returns first found matching node.
     *
     * @param node     The parent node
     * @param nodeName The child node's element name
     * @return The first matching child Node
     */
    public static Node getNode(Node node, String nodeName) {
        NodeList ch = node.getChildNodes();
        int l = ch.getLength();
        for (int i = 0; i < l; i++) {
            Node n = ch.item(i);
            if (n.getNodeName().equals(nodeName)) {
                return n;
            }
        }
        return null;
    }
}

Related

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