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

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

Description

Retrieve the list of the elements which have an attribute equal to the given value.

License

GNU General Public License

Parameter

Parameter Description
root the root element
attribute the attribute name
value the value

Return

the list

Declaration

public static List<Element> getElementsWithAttributeEquals(
        Element root, String attribute, String value) 

Method Source Code

//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);
                }
            }
        }
    }
}

Related

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