Java tutorial
//package com.java2s; //License from project: Apache License import java.util.logging.Logger; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; import org.w3c.dom.NodeList; public class Main { private static Logger log = Logger.getLogger("XmlUtils"); /** * Get nodelist from XML document using xpath expression * * @param doc * @param expression * @return * @throws XPathExpressionException */ public static NodeList getNodeList(final Document doc, final String expression) throws XPathExpressionException { if (null == expression || expression.isEmpty() || null == doc) { return null; } log.finer("Executing xpath xpression " + expression); XPath xPath = XPathFactory.newInstance().newXPath(); XPathExpression expr = xPath.compile(expression); Object result = expr.evaluate(doc, XPathConstants.NODESET); return (NodeList) result; } }