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

StringgetValueByXPath(Document document, String xpath)
Get a value by xpath.
String value = "";
try {
    value = xPathFactory.newXPath().evaluate(xpath, document).trim();
} catch (Exception e) {
return value;
intgetValueByXPathAsInt(Document document, String xpath)
Get a value by xpath and return as an int.
int x = 0;
try {
    x = Integer.parseInt(getValueByXPath(document, xpath));
} catch (Exception e) {
return x;
StringgetValueFromXPath(Document document, String xpathString)
Returns the string value of evaluated xpath
try {
    return (String) XPathFactory.newInstance().newXPath().evaluate(xpathString, document,
            XPathConstants.STRING);
} catch (XPathExpressionException e) {
    throw new RuntimeException("Error evaluating xpath [" + xpathString + "] -- " + e.getMessage());
ListgetXmlElements(Document inXml, String xpath)
get Xml Elements
try {
    NodeList nodeList = (NodeList) xPath.evaluate(xpath, inXml, XPathConstants.NODESET);
    List<Element> results = new ArrayList<>();
    for (int i = 0; i < nodeList.getLength(); i++) {
        results.add((Element) nodeList.item(i));
    return results;
} catch (Exception ex) {
...
ArrayListselectElements(Element element, String xpathExpression)
select Elements
ArrayList<Element> resultVector = new ArrayList<Element>();
if (element == null) {
    return resultVector;
if (xpathExpression.indexOf("/") == -1) {
    NodeList nodeList = element.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
...
NodeIteratorselectNodeIterator(Node nContextNode, String sXPath)
Use an XPath string to select a nodelist.
try {
    return (NodeIterator) s_mSelectNodeIterator.invoke(null, new Object[] { nContextNode, sXPath });
} catch (Exception e) {
    throw new TransformerException(e);
NodeListselectNodeList(Node contextNode, String expression)
select Node List
XPath xpath = getXPathFactory().newXPath();
XPathExpression xexpr = xpath.compile(expression);
return (NodeList) xexpr.evaluate(contextNode, XPathConstants.NODESET);
NodeListselectNodeList(Node node, String expression)
select Node List
XPathFactory factory = XPathFactory.newInstance();
XPath path = factory.newXPath();
Object result = path.evaluate(expression, node, XPathConstants.NODESET);
return (NodeList) result;
ListselectNodes(final Node node, final String xPath)
select Nodes
return wrapNodeList((NodeList) _xpf.newXPath().evaluate(xPath, node, XPathConstants.NODE));
NodeListselectNodes(Node nodeParent, String name)
select Nodes
try {
    XPath xpath = XPathFactory.newInstance().newXPath();
    return (NodeList) xpath.evaluate(name, nodeParent, XPathConstants.NODESET);
} catch (Exception e) {
    return null;