Java Utililty Methods XPath Get

List of utility methods to do XPath Get

Description

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

Method

Node[]getNodeListAsArray(Node doc, String xpath)
get Node List As Array
try {
    NodeList list = XPathAPI_selectNodeList(doc, xpath);
    int length = list.getLength();
    if (length > 0) {
        Node[] array = new Node[length];
        for (int i = 0; i < length; i++) {
            array[i] = list.item(i);
        return array;
    return null;
} catch (TransformerException ex) {
    throw new RuntimeException(ex);
ArrayListgetNodeListAttValAsStringCol(final String xPath, final Node node, final String attrName)
get Node List Att Val As String Col
ArrayList<String> retV = new ArrayList<String>();
NodeList nl = getNodesListXpathNode(xPath, node);
int l = nl.getLength();
Element e = null;
String val = "";
for (int i = 0; i < l; i++) {
    e = (Element) nl.item(i);
    if (e.getNodeType() == Node.ELEMENT_NODE) {
...
ListgetNodes(Node node, String expStr)
get Nodes
final List<Node> nodes = new ArrayList<Node>();
final XPathFactory fac = XPathFactory.newInstance();
final XPath xpath = fac.newXPath();
final XPathExpression exp = xpath.compile(expStr);
final NodeList nl = (NodeList) exp.evaluate(node, XPathConstants.NODESET);
for (int i = 0; i < nl.getLength(); i++) {
    nodes.add(nl.item(i));
return nodes;
NodeListgetNodesByPath(String path, Element localElement, Document doc)
get Nodes By Path
XPath xpath = factory.newXPath();
Object element = path.startsWith("/") || localElement == null ? doc : localElement;
NodeList nodeList = (NodeList) xpath.evaluate(path, element, XPathConstants.NODESET);
return nodeList;
NodeListgetNodesByXPath(Document doc, XPathExpression expr)
Get xml nodes by xpath expression
return (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
ListgetNodesByXPath(Element parent, String name)
get Nodes By X Path
List<Node> result = new ArrayList<>();
NodeList nodeList = findNodesByXPath(parent, name);
for (int i = 0, l = nodeList.getLength(); i < l; i++) {
    result.add(nodeList.item(i));
return result;
NodeListgetNodesListXpathNode(final String xPath, final Node node)
get Nodes List Xpath Node
return (NodeList) getNodesListXpath(xPath, node, "", "", XPathConstants.NODESET);
StringgetNodeText(XPath xpath, String xp, Node n)
Return text content of a node, or null if it has none.
Node p = (Node) xpath.evaluate(xp, n, XPathConstants.NODE);
if (p == null)
    return null;
String s = p.getNodeValue();
return (s == null) ? null : s.trim();
StringgetNodeTextByXPath(Document doc, String xpath)
get Node Text By X Path
try {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpathEn = factory.newXPath();
    return xpathEn.evaluate(xpath, doc);
} catch (Exception ex) {
    ex.printStackTrace();
    return "";
NodeListgetNogeList(String path, Node node)
get Noge List
path = path.trim();
try {
    XPath xp = XPathFactory.newInstance().newXPath();
    XPathExpression expr = xp.compile(path);
    NodeList lst = (NodeList) expr.evaluate(node, XPathConstants.NODESET);
    return lst;
} catch (Exception ex) {
    throw new RuntimeException(ex);
...