Java XML Attribute from Node getNodeAttr(String tagName, String attrName, NodeList nodes)

Here you can find the source of getNodeAttr(String tagName, String attrName, NodeList nodes)

Description

get Node Attr

License

Apache License

Declaration

static public String getNodeAttr(String tagName, String attrName, NodeList nodes) 

Method Source Code

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

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

public class Main {
    static public String getNodeAttr(String attrName, Node node) {
        NamedNodeMap attrs = node.getAttributes();
        for (int y = 0; y < attrs.getLength(); y++) {
            Node attr = attrs.item(y);
            if (attr.getNodeName().equalsIgnoreCase(attrName)) {
                return attr.getNodeValue();
            }/*from   w w w  .j a v  a  2s  .co m*/
        }
        return "";
    }

    static public String getNodeAttr(String tagName, String attrName, NodeList nodes) {
        for (int x = 0; x < nodes.getLength(); x++) {
            Node node = nodes.item(x);
            if (node.getNodeName().equalsIgnoreCase(tagName)) {
                NodeList childNodes = node.getChildNodes();
                for (int y = 0; y < childNodes.getLength(); y++) {
                    Node data = childNodes.item(y);
                    if (data.getNodeType() == Node.ATTRIBUTE_NODE) {
                        if (data.getNodeName().equalsIgnoreCase(attrName)) {
                            return data.getNodeValue();
                        }
                    }
                }
            }
        }

        return "";
    }

    static public String getNodeValue(Node node) {
        NodeList childNodes = node.getChildNodes();
        for (int x = 0; x < childNodes.getLength(); x++) {
            Node data = childNodes.item(x);
            if (data.getNodeType() == Node.TEXT_NODE) {
                return data.getNodeValue();
            }
        }
        return "";
    }

    static public String getNodeValue(String tagName, NodeList nodes) {
        for (int x = 0; x < nodes.getLength(); x++) {
            Node node = nodes.item(x);
            if (node.getNodeName().equalsIgnoreCase(tagName)) {
                NodeList childNodes = node.getChildNodes();
                for (int y = 0; y < childNodes.getLength(); y++) {
                    Node data = childNodes.item(y);
                    if (data.getNodeType() == Node.TEXT_NODE) {
                        return data.getNodeValue();
                    }
                }
            }
        }
        return "";
    }
}

Related

  1. getLongAttributeByName(Node node, String name, long defaultValue)
  2. getLowerAttrValue(Node node, String attr)
  3. getNextSiblingElement(Node node, String elemName, String attrName, String attrValue)
  4. getNodeAttr(String attrName, Node node)
  5. getNodeAttr(String attrName, Node node)
  6. getNodeAttribute(Node n, String as)
  7. getNodeAttribute(Node n, String attrName)
  8. getNodeAttribute(Node n, String name)
  9. GetNodeAttribute(Node node, String attributeName)