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

ByteasByte(String expression, Node node)
Evaluates the specified XPath expression and returns the result as a Byte.
String byteString = evaluateAsString(expression, node);
return (isEmptyString(byteString)) ? null : Byte.valueOf(byteString);
ByteasByte(String expression, Node node)
Evaluates the specified XPath expression and returns the result as a Byte.
String byteString = evaluateAsString(expression, node);
return (isEmptyString(byteString)) ? null : Byte.valueOf(byteString);
StringconvertToXpath(String qname)
convert To Xpath
QName name = QName.valueOf(qname);
if ("".equals(name.getNamespaceURI())) {
    return "//" + name.getLocalPart();
} else {
    return "//*[local-name()='" + name.getLocalPart() + "' and namespace-uri()='" + name.getNamespaceURI()
            + "']";
Nodeevaluate(Document doc)
evaluate
return (Node) expression.evaluate(doc, XPathConstants.NODE);
Stringevaluate(File xmlFile, String xPathExpression)
evaluate
String result = null;
try {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xPath = factory.newXPath();
    result = xPath.evaluate(xPathExpression, new InputSource(new FileInputStream(xmlFile)));
} catch (Exception ex) {
    System.out.println(ex.getMessage());
return result;
booleanevaluate(Node node, XPathExpression expression)
Evaluates the XPath expression in the specified context and returns whether such element was found.
try {
    Boolean result = (Boolean) expression.evaluate(node, XPathConstants.BOOLEAN);
    return result != null && result;
} catch (XPathExpressionException e) {
    return false;
Tevaluate(Object obj, String xpathExpression, QName qName)
evaluate
try {
    return (T) xpath.compile(xpathExpression).evaluate(obj, qName);
} catch (XPathExpressionException e) {
    throw new RuntimeException(e);
Stringevaluate(String path, Node node)
evaluate
path = path.trim();
try {
    XPath xp = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xp.compile(path);
    String ret = expr.evaluate(node);
    return ret;
} catch (Exception ex) {
    throw new RuntimeException(ex);
...
Stringevaluate(XPath xpath, String base, String path, Document document)
evaluate
try {
    return xpath.evaluate(base + "/" + path, document).trim();
} catch (Exception e) {
return null;
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;
if (!expression.equals(".")) {
    if (asNode(expression, node) == null)
        return null;
String s = xpath().evaluate(expression, node);
return s.trim();
...