XpathUtils { public static Date asDate(String expression, Node node) throws XPathExpressionException { return asDate(expression, node, xpath()); }  static XPath xpath(); static Document documentFrom(InputStream is); static Document documentFrom(String xml); static Document documentFrom(URL url); static Double asDouble(String expression, Node node); static Double asDouble(String expression, Node node, XPath xpath); static String asString(String expression, Node node); static String asString(String expression, Node node, XPath xpath); static Integer asInteger(String expression, Node node); static Integer asInteger(String expression, Node node, XPath xpath); static Boolean asBoolean(String expression, Node node); static Boolean asBoolean(String expression, Node node, XPath xpath); static Float asFloat(String expression, Node node); static Float asFloat(String expression, Node node, XPath xpath); static Long asLong(String expression, Node node); static Long asLong(String expression, Node node, XPath xpath); static Byte asByte(String expression, Node node); static Byte asByte(String expression, Node node, XPath xpath); static Date asDate(String expression, Node node); static Date asDate(String expression, Node node, XPath xpath); static ByteBuffer asByteBuffer(String expression, Node node); static ByteBuffer asByteBuffer(String expression, Node node, XPath xpath); static boolean isEmpty(Node node); static Node asNode(String nodeName, Node node); static Node asNode(String nodeName, Node node, XPath xpath); static int nodeLength(NodeList list);  }
@Test public void testAsDate() throws Exception { Calendar expectedDate = new GregorianCalendar(); expectedDate.set(Calendar.YEAR, 2008); expectedDate.set(Calendar.MONTH, Calendar.OCTOBER); expectedDate.set(Calendar.DAY_OF_MONTH, 7); expectedDate.set(Calendar.AM_PM, Calendar.AM); expectedDate.set(Calendar.HOUR, 11); expectedDate.set(Calendar.MINUTE, 51); expectedDate.set(Calendar.SECOND, 50); expectedDate.set(Calendar.MILLISECOND, 0); expectedDate.setTimeZone(new SimpleTimeZone(0, "UTC")); Document document = documentFrom(DOCUMENT); XPath xpath = xpath(); Date date = XpathUtils.asDate("Foo/Since", document, xpath); assertNotNull(date); assertEquals(expectedDate.getTimeInMillis(), date.getTime()); assertEquals(null, asDate("Foo/Empty", document, xpath)); }