Java tutorial
//package com.java2s; import javax.xml.namespace.QName; 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 evaluateToNodeList(Document document, String expression) throws XPathExpressionException { return (NodeList) evaluate(document, expression, XPathConstants.NODESET); } public static Object evaluate(Document document, String expression, QName returnType) throws XPathExpressionException { XPathExpression xpathexp = getXPathExpression(expression); return xpathexp.evaluate(document, returnType); } public static XPathExpression getXPathExpression(String expression) throws XPathExpressionException { return getXPathInstance().compile(expression); } public static XPath getXPathInstance() { XPath xPath = XPathFactory.newInstance().newXPath(); return xPath; } }