Java tutorial
//package com.java2s; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import javax.xml.namespace.NamespaceContext; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathFactory; public class Main { /** * @param node An XML DOM Tree for query * @param query An XPATH query to run against the DOM Tree * @param nc The namespaceContext that maps prefixes to XML namespace * @return A list of nodes that result from running the query against * the node. * @throws Exception If anything goes wrong. No error handling for brevity */ public static NodeList query(Node node, String query, NamespaceContext nc) throws Exception { XPathFactory xpf = XPathFactory.newInstance(); XPath xpath = xpf.newXPath(); xpath.setNamespaceContext(nc); return (NodeList) xpath.evaluate(query, node, XPathConstants.NODESET); } }