Java tutorial
//package com.java2s; import java.io.StringReader; 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; import org.xml.sax.InputSource; public class Main { public static String xPathEvalSingle(String content, String expression) throws XPathExpressionException { List<String> result = xPathEvalList(content, expression); if (result.size() > 0) return result.get(0); else return null; } public static List<String> xPathEvalList(String content, String expression) throws XPathExpressionException { List<String> retValues = new ArrayList<String>(); XPath xpath = XPathFactory.newInstance().newXPath(); InputSource inputSource = new InputSource(new StringReader(content)); NodeList ox = (NodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET); for (int i = 0; i < ox.getLength(); i++) { retValues.add(ox.item(i).getNodeValue()); } return retValues; } }