XPathFinder { public NodeList query(String query) { try { XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); XPathExpression expr = xpath.compile(query); NodeList result = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); return result; } catch (XPathExpressionException e) { throw new RuntimeException(e); } }  Document getDoc(); void setDoc(Document doc); void config(WebResponse response); NodeList query(String query); List<String> queryTexts(String query); List<Long> queryLongs(String query);  }
@Test public void testQuery() throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); ByteArrayInputStream in = new ByteArrayInputStream( "<a><b>2</b><b>4</b></a>".getBytes()); Document doc = builder.parse(in); XPathFinder finder = new XPathFinder(); finder.setDoc(doc); NodeList ret = finder.query(" assertEquals("2", ret.item(0).getFirstChild().getNodeValue()); assertEquals("4", ret.item(1).getFirstChild().getNodeValue()); assertEquals(2, ret.getLength()); }