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

StringgetString(final String xPath, final Object item)
Evaluates the xpath on an object
return (String) evaluateXPath(xPath, item, XPathConstants.STRING);
StringgetString(Node node, XPathExpression expr)
Get a string from an XPath expression.
return (String) expr.evaluate(node, XPathConstants.STRING);
StringgetStringByXPath(String xml, String xpathStr)
Get a string by XPATH from a xml file
Document doc = parseXML(xml);
if (doc == null)
    return null;
try {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    XPathExpression expr = xpath.compile(xpathStr);
    return (String) expr.evaluate(doc);
...
StringgetStringValue(Node node, XPathExpression expression)
Evaluates the XPath expression in the specified context and returns the found element as a String.
try {
    return (String) expression.evaluate(node, XPathConstants.STRING);
} catch (XPathExpressionException e) {
    return null;
StringgetStringValue(Object doc, XPathExpression path)
get String Value
return path.evaluate(doc);
StringgetStringValue(String targetDoc, String xpathExp, String encoding)
get String Value
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new ByteArrayInputStream(targetDoc.getBytes(encoding)));
XPathFactory pathFactory = XPathFactory.newInstance();
XPath xpath = pathFactory.newXPath();
XPathExpression pathExpression = xpath.compile(xpathExp);
String result = (String) pathExpression.evaluate(doc, XPathConstants.STRING);
if (result != null) {
    result = result.trim();
...
StringgetStringValueByXPath(Node node, String xPath)
get String Value By X Path
return path.evaluate(xPath, node);
StringgetText(String xPathExpression, Node node)
get Text
XPath xpath = xPathFactory.newXPath();
return xpath.evaluate(xPathExpression, node);
String[]getTextNodes(Node contextNode, String xPath)
get Text Nodes
return getTextNodes(contextNode, xPath, contextNode);
StringgetValidXpath(String xPath, NamespaceContext context)
get Valid Xpath
if (context == null)
    return xPath;
String namespaceURI = context.getNamespaceURI(DEFAULT_NS);
if (namespaceURI != null && !namespaceURI.isEmpty()) {
    StringBuilder stringBuilder = new StringBuilder();
    String[] tags = xPath.split("/");
    int length = tags.length;
    for (int i = 0; i < length - 1; i++) {
...