Java XML Element Get by Attribute getElementByAttribute(String attr, String value, Element root)

Here you can find the source of getElementByAttribute(String attr, String value, Element root)

Description

get Element By Attribute

License

Apache License

Declaration

public final static Element getElementByAttribute(String attr, String value, Element root) 

Method Source Code

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

import org.w3c.dom.Element;

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

public class Main {
    public final static Element getElementByAttribute(String attr, String value, Element root) {
        Element found = null;//w  ww  .ja va 2 s.  co m
        if (root.hasAttribute(attr) && root.getAttribute(attr).equals(value)) {
            return root;
        }
        NodeList nl = root.getChildNodes();
        for (int i = nl.getLength() - 1; i >= 0; i--) {
            if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) {
                found = getElementByAttribute(attr, value, (Element) nl.item(i));
                if (found != null) {
                    return found;
                }
            }
        }
        return null;
    }
}

Related

  1. findElementWithNameAttribute(Element parent, String name)
  2. findElementWithUniqueAttribute(Element root, String elementName, String attribute, String attributeValue)
  3. findNode(Node node, String attr, String value)
  4. findNodeByAttributeValue(NodeList nodeList, String attributeName, String attributeValue)
  5. getElementByAttribute(Element root, String tagname, String attribute, String att_value)
  6. getElementByAttributeValue(List elements, String attName, String attValue)
  7. getElementByAttributeValue(Node start, String tagName, String attrName, String attrValue)
  8. getElementIntAttribute(Element e, String whichAttribute)
  9. getElementsWithAttribute(Element element, String attribute)