XmlUtils { static <T> List<T> getListFromDocument(final Document vastDoc, final String elementName, final String attributeName, final String attributeValue, NodeProcessor<T> nodeProcessor) { final ArrayList<T> results = new ArrayList<T>(); if (vastDoc == null) { return results; } final NodeList nodes = vastDoc.getElementsByTagName(elementName); if (nodes == null) { return results; } List<String> attributeValues = attributeValue == null ? null : Arrays.asList(attributeValue); for (int i = 0; i < nodes.getLength(); i++) { final Node node = nodes.item(i); if (node != null && nodeMatchesAttributeFilter(node, attributeName, attributeValues)) { T processed = nodeProcessor.process(node); if (processed != null) { results.add(processed); } } } return results; } private  XmlUtils();   }
@Test public void testGetListFromDocument() throws Exception { List<Date> orderDates = XmlUtils.getListFromDocument(testDoc, "aw:PurchaseOrder", "aw:OrderDate", null, new XmlUtils.NodeProcessor<Date>() { DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.US); @Override public Date process(final Node node) { try { return formatter.parse(node.getAttributes().getNamedItem("aw:OrderDate").getNodeValue()); } catch (ParseException e) { return null; } } }); assertThat(orderDates.size()).isEqualTo(3); assertThat(orderDates.get(0).getYear()).isEqualTo(99); assertThat(orderDates.get(0).getMonth()).isEqualTo(9); assertThat(orderDates.get(0).getDate()).isEqualTo(20); assertThat(orderDates.get(1).getYear()).isEqualTo(99); assertThat(orderDates.get(1).getMonth()).isEqualTo(9); assertThat(orderDates.get(1).getDate()).isEqualTo(22); assertThat(orderDates.get(2).getYear()).isEqualTo(99); assertThat(orderDates.get(2).getMonth()).isEqualTo(9); assertThat(orderDates.get(2).getDate()).isEqualTo(22); }