Here you can find the source of getElement(final Document doc, final String expression)
Parameter | Description |
---|---|
doc | a parameter |
expression | a parameter |
Parameter | Description |
---|---|
XPathExpressionException | an exception |
public static Element getElement(final Document doc, final String expression) throws XPathExpressionException
//package com.java2s; //License from project: Apache License import java.util.logging.Logger; 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.Element; public class Main { private static Logger log = Logger.getLogger("XmlUtils"); /**/* w ww .j a v a 2 s . c o m*/ * Get one element from XML document using xpath expression * * @param doc * @param expression * @return * @throws XPathExpressionException */ public static Element getElement(final Document doc, final String expression) throws XPathExpressionException { if (null == expression || expression.isEmpty() || null == doc) { return null; } log.finer("Executing xpath xpression " + expression); XPath xPath = XPathFactory.newInstance().newXPath(); XPathExpression expr = xPath.compile(expression); Object result = expr.evaluate(doc, XPathConstants.NODE); return (Element) result; } }