Here you can find the source of queryXmlByXPath(Document xmlDocument, String xpathExpressionString)
public static NodeList queryXmlByXPath(Document xmlDocument, String xpathExpressionString)
//package com.java2s; 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 { public static NodeList queryXmlByXPath(Document xmlDocument, String xpathExpressionString) { try {// ww w .jav a 2s.co m // Set up xpath query XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); XPathExpression xpathExpression = xpath .compile(xpathExpressionString); // Evaluate on document and return result Object result = xpathExpression.evaluate(xmlDocument, XPathConstants.NODESET); return (NodeList) result; } catch (XPathExpressionException e) { throw new IllegalArgumentException("Not a valid xpath"); } } }