Here you can find the source of getElementsByXPath(Element parent, String name)
public static List<Element> getElementsByXPath(Element parent, String name) throws XPathExpressionException
//package com.java2s; //License from project: Open Source License import java.util.ArrayList; import java.util.List; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { public static List<Element> getElementsByXPath(Element parent, String name) throws XPathExpressionException { List<Element> result = new ArrayList<>(); for (Node node : getNodesByXPath(parent, name)) { if (node instanceof Element) { result.add((Element) node); }//from w w w . j a va 2 s . c o m } return result; } public static List<Node> getNodesByXPath(Element parent, String name) throws XPathExpressionException { 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; } public static NodeList findNodesByXPath(Node contextNode, String expression) throws XPathExpressionException { return (NodeList) XPathFactory.newInstance().newXPath().evaluate(expression, contextNode, XPathConstants.NODESET); } }