The list of methods to do XPath Find are organized into topic(s).
List
findAll(Node node, String xpath)
Returns a NodeList composed of all the nodes that match an XPath expression, which must be valid.
if (node == null) {
throw new NullPointerException("node cannot be null.");
try {
NodeList nodes = (NodeList) xPathEngine.evaluate(xpath, node, XPathConstants.NODESET);
List<Node> result = new ArrayList<Node>(nodes.getLength());
for (int i = 0; i < nodes.getLength(); i++) {
result.add(nodes.item(i));
...
List
findAll(Node root, String xPath)
Finds all nodes matching the xPath
List<Node> result = new ArrayList<Node>();
NodeList childNodes;
try {
childNodes = (NodeList) (xPath(xPath).evaluate(root, XPathConstants.NODESET));
} catch (XPathExpressionException e) {
throw new IllegalArgumentException("Invalid expression: " + xPath, e);
for (int i = 0; i < childNodes.getLength(); i += 1) {
...
Node
findNode(String xPathExpression, Element root) Checks in under a given root element whether it can find a child node which matches the XPath expression supplied.
if (xPathExpression == null || root == null || xPathExpression.length() == 0) {
throw new IllegalArgumentException("Xpath expression and root element required");
Node node = null;
try {
XPathExpression expr = compiledExpressionCache.get(xPathExpression);
if (expr == null) {
expr = xpath.compile(xPathExpression);
...
Node
findNodeByXPath(Node base, String xpath) find Node By X Path
if ("/".equals(xpath)) {
return base;
Node node = base;
StringTokenizer tok = new StringTokenizer(xpath, "/");
while (tok.hasMoreTokens()) {
String subpath = tok.nextToken();
String localName;
...
Node
findNodeByXpath(String xpathExpression, String fileName) find Node By Xpath
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
DocumentBuilder db;
Document doc;
try {
db = dbf.newDocumentBuilder();
doc = db.parse(new File(fileName));
XPath xpath = XPathFactory.newInstance().newXPath();
...
List
findNodes(final Node node, final String expression)
find Nodes
final XPath path = xpf.newXPath();
final XPathExpression expr = path.compile(expression);
final NodeList nodeList = (NodeList) expr.evaluate(node, XPathConstants.NODESET);
if (nodeList == null) {
return Collections.emptyList();
final List<Node> result = new LinkedList<Node>();
for (int i = 0; i < nodeList.getLength(); i++) {
...
NodeList
query(Node context, String query) Performs a xpath query on a document and returns the matching nodelist.
NodeList result = null;
XPath xpath = XPathFactory.newInstance().newXPath();
try {
result = (NodeList) xpath.evaluate(query, context, XPathConstants.NODESET);
} catch (XPathExpressionException xpx) {
throw new Exception("Error evaluating XPath: " + xpx.getMessage(), xpx);
return result;
...
String
querySingle(Node node, String xpathQuery, final String filePath) query Single
NodeList list = query(node, xpathQuery, filePath);
int count = list.getLength();
if (count > 1)
throw new RuntimeException(
"query " + xpathQuery + " returned " + list.getLength() + " results. Should have been one.");
return count == 1 ? list.item(0).getTextContent() : null;