Java tutorial
//package com.java2s; //License from project: Apache License import javax.xml.namespace.QName; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { public static final String XPATH_FACTORY = "net.sf.saxon.xpath.XPathFactoryImpl"; public static NodeList getNodesListXpathNode(final String xPath, final Node node) throws Exception { return (NodeList) getNodesListXpath(xPath, node, "", "", XPathConstants.NODESET); } /** * * @param xPathS * @param node * @param nsuri * @param pre * @param returnType * @return Return type is one of XPathConstants .BOOLEAN, .NODE, .NODESET, * .NUMBER, .STRING * @throws Exception */ public static Object getNodesListXpath(final String xPathS, final Node node, final String nsuri, final String pre, final QName returnType) throws Exception { Object matches = null; System.setProperty("javax.xml.xpath.XPathFactory:" + XPathConstants.DOM_OBJECT_MODEL, XPATH_FACTORY); XPathFactory xpathFactory = XPathFactory.newInstance(XPathConstants.DOM_OBJECT_MODEL); XPath xpath = xpathFactory.newXPath(); XPathExpression xpe = xpath.compile(xPathS); matches = xpe.evaluate(node, returnType); return matches; } }