Java tutorial
//package com.java2s; //License from project: Apache License import java.util.ArrayList; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Element; import org.w3c.dom.NodeList; public class Main { private static final XPathFactory XPATH_FACTORY = XPathFactory.newInstance(); public static Iterable<Element> elementsByXpath(Element sourceRoot, String xpathExpress) throws XPathExpressionException { XPath xPath = XPATH_FACTORY.newXPath(); NodeList nodes = (NodeList) xPath.evaluate(xpathExpress, sourceRoot, XPathConstants.NODESET); ArrayList<Element> elements = new ArrayList<Element>(nodes.getLength()); for (int i = 0; i < nodes.getLength(); ++i) elements.add((Element) nodes.item(i)); return elements; } }