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

NodeevaluateXPathExpressionAndReturnNode(String expression, Node node)
Evaluates a XPath expression from a XML node, returning a Node object
try {
    return (Node) xPath.get().evaluate(expression, node, XPathConstants.NODE);
} catch (XPathExpressionException e) {
    return null;
NodeListevaluateXPathExpressionAndReturnNodeList(String expression, Node node)
Evaluates a XPath expression from a XML node, returning a NodeList.
try {
    return (NodeList) getXPath().get().evaluate(expression, node, XPathConstants.NODESET);
} catch (XPathExpressionException e) {
    return null;
NodeListevaluateXPathQuery(Document doc, NamespaceContext context, String xPathQuery)
evaluate X Path Query
XPathExpression expr = createXPathExpression(context, xPathQuery);
return (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
ObjectevalXPath(Node d, String expr, QName returnType)
Gets the object at the given XPath in the given XML node.
return XPathFactory.newInstance().newXPath().compile(expr).evaluate(d, returnType);
ListevalXPath(Node node, String xPath)
eval X Path
synchronized (xpath) {
    try {
        NodeList nodes = (NodeList) xpath.evaluate(xPath, node, XPathConstants.NODESET);
        int size = nodes.getLength();
        ArrayList<Node> list = new ArrayList<Node>(size);
        for (int i = 0; i < size; i++)
            list.add(nodes.item(i));
        return list;
...
TevalXPath(String expression, Object item, Class type)
Evaluates an XPath returning null if not found.
Object val;
if (type == String.class)
    val = evalXPath(expression, item, XPathConstants.STRING);
else if (type == CharSequence.class)
    val = evalXPath(expression, item, XPathConstants.STRING);
else if (type == Boolean.class)
    val = evalXPath(expression, item, XPathConstants.BOOLEAN);
else if (type == Boolean.TYPE)
...
StringevalXPath(String path, Document doc)
eval X Path
XPath xpath = newXPath();
try {
    return xpath.evaluate(path, doc);
} catch (XPathExpressionException e) {
    throw new RuntimeException(e);
NodeListevalXpath(String xpath, Object item)
eval Xpath
XPath xp = XPathFactory.newInstance().newXPath();
XPathExpression expr = xp.compile(xpath);
return (NodeList) expr.evaluate(item, XPathConstants.NODESET);
StringevalXPathAsString(Object item, String xpath, XPathFactory factory)
eval X Path As String
XPath path = factory.newXPath();
XPathExpression expr = path.compile(xpath);
return (String) expr.evaluate(item, XPathConstants.STRING);
ListevalXPathAsStringList(Object item, String xpath, XPathFactory factory, boolean includeDuplicates)
eval X Path As String List
NodeList nl = evalXPathAsNodeList(item, xpath, factory);
List<String> result = new ArrayList<String>(nl.getLength());
for (int i = 0; i < nl.getLength(); i++) {
    String text = nl.item(i).getTextContent();
    if (includeDuplicates || !result.contains(text)) {
        result.add(text);
return result;