Java Utililty Methods XPath Evaluate

List of utility methods to do XPath Evaluate

Description

The list of methods to do XPath Evaluate are organized into topic(s).

Method

StringevaluateAsString(String expression, Node node)
Evaluates the specified expression on the specified node and returns the result as a String.
if (isEmpty(node))
    return null;
String s = evaluateXPath(node, expression);
if (s == null) {
    return null;
} else {
    return s.trim();
ListevaluateNodeListTextXPath(final Document document, final String expression)
evaluate Node List Text X Path
return evaluateNodeListTextXPath(document, compile(expression));
ListevaluateNodeListXPath(final Document document, final String expression)
evaluate Node List X Path
return evaluateNodeListXPath(document, compile(expression));
NodeListevaluateNodes(final XPathExpression xpathExpression, final Document pom)
evaluate Nodes
try {
    return (NodeList) xpathExpression.evaluate(pom, XPathConstants.NODESET);
} catch (final Exception e) {
    throw new IllegalStateException(e.getMessage(), e);
NodeListevaluateNodeSet(XPathExpression xpe, Object item)

Evaluates the given XPath expression in the context of the given item, with the expectation of a node set result.

return (NodeList) xpe.evaluate(item, XPathConstants.NODESET);
ObjectevaluateXpath(Document doc, String xpath, QName returnType)
evaluate Xpath
XPath path = xpf.newXPath();
return path.evaluate(xpath, doc, returnType);
NodeListevaluateXPath(Element element, String xpathstring)
Purpose: Evaluates an XPath and returns the Node list associated witht the XPath
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile(xpathstring);
NodeList result = (NodeList) expr.evaluate(element, XPathConstants.NODESET);
return result;
NodeListevaluateXpath(Element from, String xpath)
Evaluates an Xpath expression and return its result as a NodeList.
XPathExpression ex = XPathFactory.newInstance().newXPath().compile(xpath);
return (NodeList) ex.evaluate(from, XPathConstants.NODESET);
ObjectevaluateXPath(final String xPath, final Object item, final QName returnType)
Evaluates the xpath on an object
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xpath.compile(xPath);
return expr.evaluate(item, returnType);
ObjectevaluateXPath(final XPathExpression expr, final Object rootNode, final QName returnType)
evaluate X Path
try {
    return expr.evaluate(rootNode, returnType);
} catch (XPathExpressionException e) {
    throw new IllegalStateException("Error while evaluating xpath expression " + expr, e);