Java XPath Expression xPathStr(String expr, Object context)

Here you can find the source of xPathStr(String expr, Object context)

Description

x Path Str

License

Open Source License

Return

the string obtained from evaluating an XPath expression

Declaration

public static String xPathStr(String expr, Object context) 

Method Source Code

//package com.java2s;
// modify it under the terms of the GNU General Public License

import javax.xml.xpath.XPath;

import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

public class Main {
    private static ThreadLocal<XPath> XPATH_POOL = new ThreadLocal<XPath>() {
        @Override/*from ww  w. jav a2  s .  com*/
        protected XPath initialValue() {
            return XPathFactory.newInstance().newXPath();
        }
    };

    /**
     * @return the string obtained from evaluating an XPath expression
     * @since 1.14.1
     */
    public static String xPathStr(String expr, Object context) {
        try {
            return xPath().evaluate(expr, context);
        } catch (XPathExpressionException e) {
            throw new IllegalArgumentException(expr, e);
        }
    }

    /**
     * @return an XPath object that can be used by the current thread
     * @since 1.14.1
     */
    public static XPath xPath() {
        return XPATH_POOL.get();
    }
}

Related

  1. streamNodes(String xpath, Object node)
  2. string(Node context, String expression)
  3. string(String fileName, String xpathExpression)
  4. valueOf(Node node, String xpath, NamespaceContext context)
  5. xmlValueOf(Node node, String xpathExpression)
  6. xpathToNode(String xpathQuery, Object domObject)
  7. XPathValueFromString(String sIn, String sxpath)