Java XML Element Get by Attribute getElementsWithAttributeEquals(Element root, String attribute, String value, List list)

Here you can find the source of getElementsWithAttributeEquals(Element root, String attribute, String value, List list)

Description

Retrieve the list of the elements which have an attribute equal to the given value (recursive function).

License

Open Source License

Parameter

Parameter Description
root the root element
attribute the attribute name
value the value
list the list to fill

Declaration

private static final void getElementsWithAttributeEquals(Element root, String attribute, String value,
        List<Element> list) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;

import java.util.List;

import org.w3c.dom.Element;

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

public class Main {
    /**/*from   w ww  .  ja v a2 s .c  o m*/
     * Retrieve the list of the elements which have an attribute equal to the given value.
     * @param root the root element
     * @param attribute the attribute name
     * @param value the value
     * @return the list
     */
    public static List<Element> getElementsWithAttributeEquals(Element root, String attribute, String value) {
        List<Element> list = new ArrayList<Element>();
        getElementsWithAttributeEquals(root, attribute, value, list);

        return list;
    }

    /**
     * Retrieve the list of the elements which have an attribute equal to the given value (recursive function).
     * @param root the root element
     * @param attribute the attribute name
     * @param value the value
     * @param list the list to fill
     */
    private static final void getElementsWithAttributeEquals(Element root, String attribute, String value,
            List<Element> list) {
        if (root.getAttribute(attribute).equals(value)) {
            list.add(root);
        }
        if (root.hasChildNodes()) {
            NodeList nodes = root.getChildNodes();
            int length = nodes.getLength();
            for (int i = 0; i < length; i++) {
                Node node = nodes.item(i);
                if (node instanceof Element) {
                    Element elem = (Element) nodes.item(i);
                    getElementsWithAttributeEquals(elem, attribute, value, list);
                }
            }
        }
    }
}

Related

  1. getElementByAttributeValue(Node start, String tagName, String attrName, String attrValue)
  2. getElementIntAttribute(Element e, String whichAttribute)
  3. getElementsWithAttribute(Element element, String attribute)
  4. getElementsWithAttribute(Element element, String namespaceURI, String localName, String value, Collection elements)
  5. getElementsWithAttributeEquals( Element root, String attribute, String value)
  6. getElementWithAttribute(String tagName, String attribute, String attributeValue, Element searchIn)
  7. getElementWithAttributeNS(Element element, String namespaceURI, String attribute, String value)
  8. selectElementsByAttributeValue(Element element, String name, String attribute, String value, boolean returnFirst)
  9. selectFirstElementByAttributeValueNS( String namespace, Element element, String name, String attribute, String value)