Java examples for XML:XPath
Return a list of elements based on xPath
//package com.java2s; 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 { /**/*from w w w . ja v a2 s . c om*/ * Return a list of elements based on xPath */ static NodeList getElementsByAttribute(Document doc, String xPath) { XPathFactory xPathfactory = XPathFactory.newInstance(); XPath xpath = xPathfactory.newXPath(); XPathExpression expr = null; NodeList nl = null; try { expr = xpath.compile(xPath); nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); } catch (XPathExpressionException e) { e.printStackTrace(); } return nl; } }