Groovy, with XMLParser and XMLSlurper and their associated classes, makes this amazingly, dramatically easier. Simple and Sane. For example, Making Java Groovy Chapter 2 has an example to parse the Google geocoder XML data to retrieve latitude and longitude. Below is the essentials of the code. The full code, which is not much longer, is on GitHub here.
The parsing is trivial, and navigating to the data (location.lat or location.lng) is also simple, following the familiar dot notation.
String url = 'http://maps.google.com/maps/api/geocode/xml?' + somemore... def response = new XmlSlurper().parse(url) stadium.latitude = response.result.geometry.location.lat.toDouble() stadium.longitude = response.result.geometry.location.lng.toDouble()
Can you do something anything like this in pure Java? Not quite. So I wrote a small library, xen, to mimic much of how Groovy does things. The full Geocoder.java code is here, snippet below:
Pretty close, eh?
String url = BASE + URLEncoder.encode(address); Xen response = new XenParser().parse(url); Option 1: XPath slash style, 1 based indices latLng = response.toDouble("result/geometry/location/lat"); latLng = response.one("result/geometry/location/lng").toDouble(); Option 2: Groovy dot style, 0 based indices latLng = response.toDouble(".result.geometry.location.lat"); latLng = response.one(".result.geometry.location.lng").toDouble();
The main difference is that we can't use the dot notation directly from an object, but we can use a very similar slash notation based upon XPath syntax. If you use XPath notation, one major difference from Groovy is that array indices in W3C XPath are 1-based, not 0-based. Therefore note that we access the 1st element of result, not the 0th. However, if the "path" starts with a . and a letter, as in the final example, the path is treated as a Groovy / "dot notation" style, with 0-based indices.
So, if you want to greatly simplify parsing and navigating through XML, and/or you love how Groovy does things, please check out my (very beta!) xen library which allows you to do it in Java. Currently it is compiled vs. Java 6 but I think it should be fine in Java 5. So if you need to support some Android device, or can't or don't want to integrate Groovy into your Java projects, this could be very useful.
The README discusses various design decisions, particularly, how my design converged upon many aspects of the Groovy design. More discussion will appear in later posts. And, be warned, this is still a very early version, 0.0.2, so there are probably bugs, some mistakes, and upcoming API changes.