public static List<? extends Node> evaluateXPath(Document doc,String path) throws XPathExpressionException {
  XPathFactory factory=XPathFactory.newInstance();
  XPath xpath=factory.newXPath();
  xpath.setNamespaceContext(new NamespaceResolver(doc));
  XPathExpression expr=xpath.compile(path);
  NodeList nodes=(NodeList)expr.evaluate(doc,XPathConstants.NODESET);
  List<Node> nodelist=new ArrayList<Node>();
  for (int i=0; i < nodes.getLength(); ++i) {
    nodelist.add(nodes.item(i));
  }
  LOG.trace(String.format("Evaluated XPath: %s and found %s",path,nodeListToString(nodelist)));
  return nodelist;
}

public static List < ? extends Node > evaluateXPath ( Document doc , String path ) throws XPathExpressionException { XPathFactory factory = XPathFactory . newInstance ( ) ; XPath xpath = factory . newXPath ( ) ; xpath . setNamespaceContext ( new NamespaceResolver ( doc ) ) ; XPathExpression expr = xpath . compile ( path ) ; NodeList nodes = ( NodeList ) expr . evaluate ( doc , XPathConstants . NODESET ) ; List < Node > nodelist = new ArrayList < Node > ( ) ; for ( int i = 0 ; i < nodes . getLength ( ) ; ++ i ) { nodelist . add ( nodes . item ( i ) ) ; } LOG . trace ( String . format ( "Evaluated XPath: %s and found %s" , path , nodeListToString ( nodelist ) ) ) ; return nodelist ; }
takes a document and evaluates an xpath expression on it . all matching nodes are returned as a list .
