Java XML Element Get findElement(Document doc, String elementNS, String elementName, String attrName, String attrValue)

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

Description

find Element

License

Apache License

Declaration

public static Element findElement(Document doc, 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 w  w . j  a va  2  s .co 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. findElement(Document doc, String tagName, Properties props)
  2. findElement(String name, Document doc)
  3. findElementAndSetElseCreateAndSet(Document document, Element parent, String child, boolean value)
  4. findElementElseCreateAndSet(Document document, Element parent, String child, boolean value)