Java tutorial
//package com.java2s; //License from project: Open Source License import java.io.IOException; 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.Node; import org.w3c.dom.NodeList; public class Main { /** * Use an Xpath expression on the given node or document. * * @param _xpathExpression xpath expression string * @param _xmlDocumentOrNode a {@link Document} or {@link Node} object * @return NodeList never null * @throws IOException on error */ public static NodeList applyXpathExpressionToDocument(String _xpathExpression, Node _xmlDocumentOrNode) throws IOException { XPathFactory xfactory = XPathFactory.newInstance(); XPath xpath = xfactory.newXPath(); XPathExpression expr = null; try { expr = xpath.compile(_xpathExpression); } catch (XPathExpressionException _ex) { throw new IOException(_ex); } Object result = null; try { result = expr.evaluate(_xmlDocumentOrNode, XPathConstants.NODESET); } catch (Exception _ex) { throw new IOException(_ex); } return (NodeList) result; } }