Java Utililty Methods XPath Get

List of utility methods to do XPath Get

Description

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

Method

StringgetValue(final String expression, final String xml)
Evaluates the XPath expression against the xml and returns the selected value.
final InputSource source = new InputSource(new StringReader(xml));
final XPathFactory factory = XPathFactory.newInstance();
final XPath xpath = factory.newXPath();
return xpath.evaluate(expression, source);
StringgetValueByPath(Node node, String path)
get Value By Path
XPathExpression expr = compile(path);
try {
    return expr.evaluate(node);
} catch (Exception e) {
    throw new RuntimeException(e);
StringgetValueByXpath(Node doc, String xquery)
get Value By Xpath
try {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    XPathExpression expression = xpath.compile(xquery);
    String result = (String) expression.evaluate(doc, XPathConstants.STRING);
    return result;
} catch (Exception e) {
return null;
StringgetValueFromXML(final String inputXML, final String xPathQuery, final int index)
get Value From XML
return getValueFromXML(inputXML, xPathQuery, index, null);
ListgetValues(final String expression, final String xml)
Evaluates the XPath expression against the xml and returns the selected values.
final NodeList xpathResults = getNodes(expression, xml);
final List<String> results = new ArrayList<String>(xpathResults.getLength());
for (int i = 0; i < xpathResults.getLength(); i++) {
    results.add(xpathResults.item(i).getNodeValue());
return results;
XPathgetX_PATH()
get PATH
if (X_PATH == null) {
    X_PATH = XPathFactory.newInstance().newXPath();
return X_PATH;
NodexGetNode(Node targetNode, String expression)
Uses the passed XPath expression to locate a single node in the passed element.
Node node = null;
XPath xpath = null;
try {
    xpath = XPathFactory.newInstance().newXPath();
    XPathExpression xpathExpression = xpath.compile(expression);
    node = (Node) xpathExpression.evaluate(targetNode, NODE);
    return node;
} catch (Exception e) {
...