Java XML Element Get by Attribute findNode(Node node, String attr, String value)

Here you can find the source of findNode(Node node, String attr, String value)

Description

Returns the first node where attr equals value.

License

Open Source License

Declaration

public static Node findNode(Node node, String attr, String value) 

Method Source Code

//package com.java2s;

import org.w3c.dom.Element;

import org.w3c.dom.Node;

public class Main {
    /**/*from  ww w  .j  a v  a2  s.c o m*/
     * Returns the first node where attr equals value. This implementation does
     * not use XPath.
     */
    public static Node findNode(Node node, String attr, String value) {
        String tmp = (node instanceof Element) ? ((Element) node)
                .getAttribute(attr) : null;

        if (tmp != null && tmp.equals(value)) {
            return node;
        }

        node = node.getFirstChild();

        while (node != null) {
            Node result = findNode(node, attr, value);

            if (result != null) {
                return result;
            }

            node = node.getNextSibling();
        }

        return null;
    }
}

Related

  1. findElementByAttribute(final NodeList list, final String name, final String value)
  2. findElementsByAttribute(Element node, String tagName, String attrName, List attrValues)
  3. findElementWithAttributes(Node node, String tagName, Collection attrs)
  4. findElementWithNameAttribute(Element parent, String name)
  5. findElementWithUniqueAttribute(Element root, String elementName, String attribute, String attributeValue)
  6. findNodeByAttributeValue(NodeList nodeList, String attributeName, String attributeValue)
  7. getElementByAttribute(Element root, String tagname, String attribute, String att_value)
  8. getElementByAttribute(String attr, String value, Element root)
  9. getElementByAttributeValue(List elements, String attName, String attValue)