Here you can find the source of getElementsWithAttributeEquals( Element root, String attribute, String value)
Parameter | Description |
---|---|
root | the root element |
attribute | the attribute name |
value | the value |
public static List<Element> getElementsWithAttributeEquals( Element root, String attribute, String value)
//package com.java2s; /*/* w w w . java 2 s .co m*/ * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab * Copyright (C) 2010 - Calixte DENIZET * * Copyright (C) 2012 - 2016 - Scilab Enterprises * * This file is hereby licensed under the terms of the GNU GPL v2.0, * pursuant to article 5.3.4 of the CeCILL v.2.1. * This file was originally licensed under the terms of the CeCILL v2.1, * and continues to be available under such terms. * For more information, see the COPYING file which you should have received * along with this program. * */ 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 { /** * 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); } } } } }