Here you can find the source of getElementByXpath(Element sourceRoot, String xpathExpress)
public static Element getElementByXpath(Element sourceRoot, String xpathExpress) throws XPathExpressionException
//package com.java2s; //License from project: Apache License import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Element; import org.w3c.dom.NodeList; public class Main { private static final XPathFactory XPATH_FACTORY = XPathFactory.newInstance(); public static Element getElementByXpath(Element sourceRoot, String xpathExpress) throws XPathExpressionException { XPath xPath = XPATH_FACTORY.newXPath(); NodeList nodes = (NodeList) xPath.evaluate(xpathExpress, sourceRoot, XPathConstants.NODESET); if (nodes.getLength() > 0) return (Element) nodes.item(0); return null; }//from w w w. j av a 2 s . c o m }