Here you can find the source of getElementsWithAttributeEquals(Element root, String attribute, String value, List
Parameter | Description |
---|---|
root | the root element |
attribute | the attribute name |
value | the value |
list | the list to fill |
private static final void getElementsWithAttributeEquals(Element root, String attribute, String value, List<Element> list)
//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); } } } } }