Java XML Node Find findNodeByAttribute(Document document, String tagName, String attributeName, String attributeValue)

Here you can find the source of findNodeByAttribute(Document document, String tagName, String attributeName, String attributeValue)

Description

find Node By Attribute

License

Apache License

Declaration

public static Node findNodeByAttribute(Document document, String tagName, String attributeName,
            String attributeValue) 

Method Source Code

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

import org.w3c.dom.Document;

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

public class Main {
    public static Node findNodeByAttribute(Document document, String tagName, String attributeName,
            String attributeValue) {
        Node foundNode = null;//from   ww w  .java2s .  co m
        NodeList nodes = document.getElementsByTagName(tagName);
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            for (int j = 0; j < node.getAttributes().getLength(); j++) {
                Node attribute = node.getAttributes().item(j);
                if (attribute.getNodeName().equals(attributeName)
                        && attribute.getNodeValue().equals(attributeValue)) {
                    foundNode = node;
                    break;
                }
            }
            if (foundNode != null)
                break;
        }
        return foundNode;
    }
}

Related

  1. findElementsByName(Node node, List elements, String name)
  2. findNode(Document source, Element n)
  3. findNode(Node aNode, String aName)
  4. findNode(Node node, String nodeName)
  5. findNode(Node rootNode, String nodeName)
  6. findNodeByName(Document doc, String pathExpression)
  7. findNodeByTagName(Document document, String tagName)
  8. findNodeByXpath(org.w3c.dom.Document doc, String xpathEx)
  9. findNodeIndex(Node node)