Java XML Element Get by Attribute getElementWithAttributeNS(Element element, String namespaceURI, String attribute, String value)

Here you can find the source of getElementWithAttributeNS(Element element, String namespaceURI, String attribute, String value)

Description

Get the first element descendant having the specified attribute name and value.

License

Apache License

Parameter

Parameter Description
element Element whose children we want to search
namespaceURI Attribute namespace URI
attribute Attribute name
value Attribute value

Return

The first element having this attribute value

Declaration


public static Element getElementWithAttributeNS(Element element, String namespaceURI, String attribute,
        String value) 

Method Source Code


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

import org.w3c.dom.NodeList;
import org.w3c.dom.Element;

import org.w3c.dom.Node;

public class Main {
    /** Get the first element descendant having the specified attribute
    name and value.//from  w ww.ja  va2s. c o  m
         
    @param element Element whose children we want to search
    @param namespaceURI Attribute namespace URI
    @param attribute Attribute name
    @param value Attribute value
    @return The first element having this attribute value */

    public static Element getElementWithAttributeNS(Element element, String namespaceURI, String attribute,
            String value) {
        String attr = element.getAttributeNS(namespaceURI, attribute);
        if (attr != null && attr.equals(value)) {
            return element;
        }

        NodeList childs = element.getChildNodes();

        for (int i = 0; i < childs.getLength(); i++) {
            Node node = childs.item(i);
            if (Node.ELEMENT_NODE == node.getNodeType()) {
                Element e = getElementWithAttributeNS((Element) node, namespaceURI, attribute, value);
                if (e != null)
                    return e;
            }
        }
        return null;
    }
}

Related

  1. getElementsWithAttribute(Element element, String attribute)
  2. getElementsWithAttribute(Element element, String namespaceURI, String localName, String value, Collection elements)
  3. getElementsWithAttributeEquals( Element root, String attribute, String value)
  4. getElementsWithAttributeEquals(Element root, String attribute, String value, List list)
  5. getElementWithAttribute(String tagName, String attribute, String attributeValue, Element searchIn)
  6. selectElementsByAttributeValue(Element element, String name, String attribute, String value, boolean returnFirst)
  7. selectFirstElementByAttributeValueNS( String namespace, Element element, String name, String attribute, String value)