Java tutorial
//package com.java2s; import java.util.function.Consumer; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { private static XPathFactory xPathFactory = XPathFactory.newInstance(); public static void forEachByXPath(Node n, String expression, Consumer<Node> action) { XPath path = xPathFactory.newXPath(); try { NodeList nl = (NodeList) path.evaluate(expression, n, XPathConstants.NODESET); forEach(nl, action); } catch (XPathExpressionException e) { throw new RuntimeException(e); } } public static void forEach(NodeList nl, Consumer<Node> action) { for (int i = 0; i < nl.getLength(); i++) { action.accept(nl.item(i)); } } }