Java tutorial
//package com.java2s; //License from project: Apache License import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.traversal.NodeIterator; public class Main { /** Holds the method for executing the selectNodeIterator method. */ private static Method s_mSelectNodeIterator; /** * This method returns an array of nodes that match the passed on criteria. * * @param eRoot The root element. * @param sPath The XPath to execute. * @return The array of nodes. */ public static Node[] match(Element eRoot, String sPath) { try { NodeIterator niIter = (NodeIterator) s_mSelectNodeIterator.invoke(null, new Object[] { eRoot, sPath }); Node nNode; List<Node> lResList = new ArrayList<Node>(128); while ((nNode = niIter.nextNode()) != null) { lResList.add(nNode); } return lResList.toArray(new Node[lResList.size()]); } catch (Exception ignored) { return new Element[0]; } } }