Java tutorial
//package com.java2s; import java.util.ArrayList; import java.util.List; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { public static List<Node> getNodeList(Document doc, String nodeName) { return getNodeList(doc, nodeName, null); } public static List<Node> getNodeList(Document doc, String nodeName, Node node) { List<Node> result = new ArrayList<Node>(); if (nodeName == null || "".equals(nodeName)) return result; getNodeList(doc, nodeName, result, node); return result; } private static void getNodeList(Document doc, String nodeName, List<Node> result, Node node) { NodeList children; if (node == null) { Element root = doc.getDocumentElement(); children = root.getChildNodes(); } else { if (node.getNodeName().equals(nodeName)) { result.add(node); return; } children = node.getChildNodes(); } if (children == null) return; for (int i = 0; i < children.getLength(); i++) { getNodeList(doc, nodeName, result, children.item(i)); } } }