Java tutorial
//package com.java2s; //License from project: Open Source License 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 { public static List<Element> findAllElementsByAttributes(Element node, String tagName, String attrName, List<String> attrValues) { List<Element> result = new ArrayList<Element>(); findAllElementsByAttributes(node, tagName, attrName, attrValues, result); return result; } private static void findAllElementsByAttributes(Node node, String tagName, String attrName, List<String> attrValues, 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) { for (String value : attrValues) { if (element.getAttribute(attrName).equals(value)) { result.add(element); break; } } } findAllElementsByAttributes(currNode, tagName, attrName, attrValues, 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; } }