Java XML Element Get by Attribute getElementsWithAttribute(Element element, String attribute)

Here you can find the source of getElementsWithAttribute(Element element, String attribute)

Description

Get the elements descendant having the specified attribute name.

License

Apache License

Parameter

Parameter Description
element Element whose children we want to search
attribute Attribute name

Return

list of the elements having this attribute value

Declaration


public static LinkedList getElementsWithAttribute(Element element, String attribute) 

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.LinkedList;

public class Main {
    /** Get the elements descendant having the specified attribute
    name and value./*  www  .  ja  v  a2  s  . c o  m*/
         
    @param element Element whose children we want to search
    @param attribute Attribute name
    @param value Attribute value
    @return list of the elements having this attribute value */

    public static LinkedList getElementsWithAttribute(Element element, String attribute, String value) {
        LinkedList elements = new LinkedList();
        if (element.hasAttribute(attribute)) {
            String attr = element.getAttribute(attribute);
            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()) {
                elements = getElementsWithAttribute((Element) node, attribute, value, elements);
            }
        }
        return elements;
    }

    private static LinkedList getElementsWithAttribute(Element element, String attribute, String value,
            LinkedList elements) {
        if (element.hasAttribute(attribute)) {
            String attr = element.getAttribute(attribute);
            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()) {
                elements = getElementsWithAttribute((Element) node, attribute, value, elements);
            }
        }
        return elements;
    }

    /** Get the elements descendant having the specified attribute
    name.
         
    @param element Element whose children we want to search
    @param attribute Attribute name
    @return list of the elements having this attribute value */

    public static LinkedList getElementsWithAttribute(Element element, String attribute) {
        LinkedList elements = new LinkedList();
        if (element.hasAttribute(attribute)) {
            //       String attr = element.getAttribute(attribute);
            //       System.out.println("element : "+element+"; attribute : "+attr);
            //       System.out.println("attribute != null");
            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()) {
                elements = getElementsWithAttribute((Element) node, attribute, elements);
            }
        }
        return elements;
    }

    private static LinkedList getElementsWithAttribute(Element element, String attribute, LinkedList elements) {
        //     String attr = element.getAttribute(attribute);
        //     System.out.println("private : element : "+element+"; attribute : "+attr);
        if (element.hasAttribute(attribute)) {
            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()) {
                elements = getElementsWithAttribute((Element) node, attribute, elements);
            }
        }
        return elements;
    }
}

Related

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