Java XML Element Get by Attribute findElement(Element parent, String elementNS, String elementName, String attrName, String attrValue)

Here you can find the source of findElement(Element parent, String elementNS, String elementName, String attrName, String attrValue)

Description

find Element

License

Apache License

Declaration

public static Element findElement(Element parent, String elementNS, String elementName, String attrName,
            String attrValue) 

Method Source Code

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

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import org.w3c.dom.NodeList;

public class Main {
    public static Element findElement(Document doc, String elementNS, String elementName, String attrName,
            String attrValue) {/*  w ww  . ja v  a  2s.  c o m*/
        return findElement(doc.getDocumentElement(), elementNS, elementName, attrName, attrValue);
    }

    public static Element findElement(Element parent, String elementNS, String elementName, String attrName,
            String attrValue) {
        NodeList l = parent.getElementsByTagNameNS(elementNS, elementName);

        for (int i = 0; i < l.getLength(); i++) {
            Element e = (Element) l.item(i);
            String val = e.getAttribute(attrName);

            if (val != null && val.equals(attrValue)) {
                return e;
            }
        }

        return null;
    }
}

Related

  1. findAllElementsByAttribute(Node node, String tagName, String attrName, String attrValue)
  2. findAllElementsByAttributes(Element node, String tagName, String attrName, List attrValues)
  3. findComponent(Element element, String attributeValue)
  4. findElement(Element element, String elementName, String attributeValue)
  5. findElementByAttribute(final NodeList list, final String name, final String value)
  6. findElementsByAttribute(Element node, String tagName, String attrName, List attrValues)
  7. findElementWithAttributes(Node node, String tagName, Collection attrs)
  8. findElementWithNameAttribute(Element parent, String name)