Java tutorial
//package com.java2s; //License from project: Apache License import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import java.util.ArrayList; import java.util.List; public class Main { private static XPath xPath; public static List<Element> getXmlElements(Document inXml, String xpath) { try { NodeList nodeList = (NodeList) xPath.evaluate(xpath, inXml, XPathConstants.NODESET); List<Element> results = new ArrayList<>(); for (int i = 0; i < nodeList.getLength(); i++) { results.add((Element) nodeList.item(i)); } return results; } catch (Exception ex) { throw new RuntimeException("Could not run xpath: " + xpath, ex); } } }