Here you can find the source of getElementsWithAttribute(Element element, String attribute)
Parameter | Description |
---|---|
element | Element whose children we want to search |
attribute | Attribute name |
public static LinkedList getElementsWithAttribute(Element element, String attribute)
//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; } }