Java tutorial
//package com.java2s; // modify it under the terms of the GNU General Public License import java.util.ArrayList; import java.util.List; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.w3c.dom.NodeList; public class Main { private static ThreadLocal<XPath> XPATH_POOL = new ThreadLocal<XPath>() { @Override protected XPath initialValue() { return XPathFactory.newInstance().newXPath(); } }; static <T> List<T> xPathObjects(String expr, Object context) { try { NodeList nodes = (NodeList) xPath().evaluate(expr, context, XPathConstants.NODESET); List<T> result = new ArrayList<T>(nodes.getLength()); for (int i = 0; i < nodes.getLength(); i++) result.add((T) nodes.item(i)); return result; } catch (XPathExpressionException e) { throw new IllegalArgumentException(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(); } }