Java tutorial
//package com.java2s; /******************************************************************************* * (c) Copyright 2017 Hewlett-Packard Development Company, L.P. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Apache License v2.0 which accompany this distribution. * * The Apache License is available at * http://www.apache.org/licenses/LICENSE-2.0 * *******************************************************************************/ import org.w3c.dom.Document; import org.w3c.dom.NodeList; import javax.xml.namespace.NamespaceContext; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; public class Main { /** * Return all the nodes in an Document for a given XPath. * * @param doc the document to read from * @param pathToNode the XPath to find * @return a NodeList object * @throws XPathExpressionException if xpath exception occurred */ public static NodeList readNode(Document doc, String pathToNode, NamespaceContext ctx) throws XPathExpressionException { XPath xPath = createXpath(); xPath.setNamespaceContext(ctx); return (NodeList) xPath.evaluate(pathToNode, doc, XPathConstants.NODESET); } /** * Returns a new instance of XPath. * * @return XPath object */ private static XPath createXpath() { return XPathFactory.newInstance().newXPath(); } }