Here you can find the source of findAllElementsByAttribute(Node node, String tagName, String attrName, String attrValue)
Parameter | Description |
---|---|
node | a parameter |
tagName | a parameter |
attrName | a parameter |
attrValue | a parameter |
public static List<Element> findAllElementsByAttribute(Node node, String tagName, String attrName, String attrValue)
//package com.java2s; //License from project: Open Source License import java.util.LinkedList; import java.util.List; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { /**//from w w w .ja v a 2 s. c o m * Reqursion search in node for elements with paramantrs * @param node * @param tagName * @param attrName * @param attrValue * @return */ public static List<Element> findAllElementsByAttribute(Node node, String tagName, String attrName, String attrValue) { List<Element> result = new LinkedList<Element>(); findAllElementsByAttribute(node, tagName, attrName, attrValue, result); return result; } private static void findAllElementsByAttribute(Node node, String tagName, String attrName, String attrValue, List<Element> result) { if (node == null) { return; } NodeList nodeList = node.getChildNodes(); if (nodeList == null) { return; } for (int i = 0; i < nodeList.getLength(); ++i) { Node currNode = nodeList.item(i); Element element = checkIfElement(currNode, tagName); if (element != null && element.getAttribute(attrName).equals(attrValue)) { result.add(element); continue; } findAllElementsByAttribute(currNode, tagName, attrName, attrValue, result); } } public static final Element checkIfElement(Node node) { Element result = null; if (isElement(node)) { result = (Element) node; } return result; } public static final Element checkIfElement(Node node, String tag) { Element result = null; if (isElement(node)) { Element tmp = (Element) node; if (tag == null || tmp.getTagName().equals(tag)) { result = tmp; } } return result; } public static boolean isElement(Node node) { return node.getNodeType() == Node.ELEMENT_NODE; } }