Java Utililty Methods XPath Select

List of utility methods to do XPath Select

Description

The list of methods to do XPath Select are organized into topic(s).

Method

StringselectString(String xpath, Object node)
select String
return ((String) evaluateXPath(xpath, node, XPathConstants.STRING)).trim();
ListselectStrings(String xpath, Object node)
select Strings
List<String> values = new ArrayList<String>();
for (Node it : selectNodes(xpath, node)) {
    String textContent = getTextContent(it);
    if (textContent.length() > 0) {
        values.add(textContent);
return values;
...
StringselectXPathString(final String xPath, final Node inNode, final String nsuri, final String pre)
select X Path String
return (String) getNodesListXpath(xPath, inNode, nsuri, pre, XPathConstants.STRING);
ListxmlSelectNodes(Node node, String xpathExpression)
Oh, for a decent xml library ;).
List list = new LinkedList();
String path[] = split(xpathExpression, "/");
xmlSelectNodes0(list, node, path, 0);
return list;
NodeListXPathAPI_selectNodeList(Document doc, String xpath, Node namespaceNode)
X Path APselect Node List
XPathFactory factory = XPathFactory.newInstance();
XPath xPath = factory.newXPath();
setNamespace(xPath, xpath);
try {
    XPathExpression xPathExpression = xPath.compile(xpath);
    return (NodeList) xPathExpression.evaluate(doc, XPathConstants.NODESET);
} catch (XPathExpressionException e) {
    throw new TransformerException("Exception in XPathAPI_selectNodeList: " + xpath, e);
...
NodexpathNode(String expr, Document doc)
xpath Node
try {
    return (Node) xpath.evaluate(expr, doc, XPathConstants.NODE);
} catch (Exception e) {
    throw new RuntimeException("Cannot evaluate XPath:", e);
NodeListxpathNodeList(String expr, Document doc)
xpath Node List
return (NodeList) xpath.evaluate(expr, doc, XPathConstants.NODESET);
StringxpathOrNull(Document doc, String xp)
Evaluate an XPath against a Document, returning a String.
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
try {
    XPathExpression expr = xpath.compile(xp);
    return expr.evaluate(doc);
} catch (XPathExpressionException e) {
    return null;
StringxpathString(String expr, Document doc)
xpath String
return (String) xpath.evaluate(expr, doc, XPathConstants.STRING);
NodexpathToNode(Document document, String xpathExpression)
xpath To Node
XPath xpath = createXPath();
return (Node) xpath.evaluate(xpathExpression, document, XPathConstants.NODE);