Java XML Element Get by Attribute getElementsWithAttribute(Element element, String namespaceURI, String localName, String value, Collection elements)

Here you can find the source of getElementsWithAttribute(Element element, String namespaceURI, String localName, String value, Collection elements)

Description

get Elements With Attribute

License

Apache License

Declaration

private static void getElementsWithAttribute(Element element, String namespaceURI, String localName,
            String value, Collection elements) 

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;

import java.util.ArrayList;
import java.util.Collection;

public class Main {
    /** Get the elements descendant having the specified attribute name
    and value./* w w  w .  j a v  a  2 s . c  om*/
         
    @param element Element whose children we want to search
    @param namespaceURI Attribute's namespace
    @param localName Attribute name
    @param value Attribute value
    @return list of the elements having this attribute value */

    public static Collection getElementsWithAttribute(Element element, String namespaceURI, String localName,
            String value) {
        Collection elements = new ArrayList();
        getElementsWithAttribute(element, namespaceURI, localName, value, elements);
        return elements;
    }

    private static void getElementsWithAttribute(Element element, String namespaceURI, String localName,
            String value, Collection elements) {
        if (element.hasAttributeNS(namespaceURI, localName)) {
            String attr = element.getAttributeNS(namespaceURI, localName);
            if (attr.equals(value))
                elements.add(element);
        }

        NodeList childs = element.getChildNodes();

        for (int i = 0; i < childs.getLength(); i++) {
            Node node = childs.item(i);
            if (Node.ELEMENT_NODE == node.getNodeType())
                getElementsWithAttribute((Element) node, namespaceURI, localName, value, elements);
        }
    }
}

Related

  1. getElementByAttribute(String attr, String value, Element root)
  2. getElementByAttributeValue(List elements, String attName, String attValue)
  3. getElementByAttributeValue(Node start, String tagName, String attrName, String attrValue)
  4. getElementIntAttribute(Element e, String whichAttribute)
  5. getElementsWithAttribute(Element element, String attribute)
  6. getElementsWithAttributeEquals( Element root, String attribute, String value)
  7. getElementsWithAttributeEquals(Element root, String attribute, String value, List list)
  8. getElementWithAttribute(String tagName, String attribute, String attributeValue, Element searchIn)
  9. getElementWithAttributeNS(Element element, String namespaceURI, String attribute, String value)