Java tutorial
//package com.java2s; import java.io.ByteArrayInputStream; import java.io.InputStream; 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.NodeList; import org.xml.sax.InputSource; public class Main { public static NodeList evalXpath(InputStream xmlStream, String path) throws XPathExpressionException { InputSource inXML = new InputSource(xmlStream); XPathFactory xfactory = XPathFactory.newInstance(); XPath xpath = xfactory.newXPath(); XPathExpression expr = xpath.compile(path); Object result = expr.evaluate(inXML, XPathConstants.NODESET); return (NodeList) result; } public static NodeList evalXpath(String inputXML, String path) throws XPathExpressionException { InputStream xmlStream = new ByteArrayInputStream(inputXML.getBytes()); return evalXpath(xmlStream, path); } }