Friday, November 27, 2009

Slurping XML with Groovy

In the early days of using Java in conjunction with XML, it often seemed more difficult than it should be to use the Java programming language with the XML markup language. Besides the non-trivial and often differently implemented DOM and SAX APIs, simply finding the correct version of Xerces (and later, Crimson) without having too many conflicting versions of that library was also a common problem. This environment led to the creation and successively received JDOM project. Later developments such as the introduction of the standard Java XML parsing API of JAXP (JDK 1.4) and the inclusion of JAXB in Java SE 6 (and other Java/XML binding libraries available separately) would make parsing and working with XML in Java much easier. Groovy continues these advances in ease of Java/XML integration. In this blog post, I look at how use of Groovy's XmlSlurper makes XML parsing refreshingly easy and almost transparent.

The following simple XML code will be used to demonstrate Groovy's XmlSlurper. The XML file for this example is called RockAndRoll.xml.


<Album title="Frontiers" artist="Journey" year="1983">
<Song title="Separate Ways" peak="8" />
<Song title="Send Her My Love" peak="23" />
<Song title="Faithfully" peak="12" />
<Album title="Hysteria" artist="Def Leppard" year="1987">
<Song title="Hysteria" peak="10" />
<Song title="Animal" peak="19" />
<Song title="Women" />
<Song title="Pour Some Sugar On Me" peak="2" />
<Song title="Love Bites" peak="1" />
<Song title="Armageddon It" peak="3" />
<Song title="Rocket" peak="15" />
<Album title="The Joshua Tree" artist="U2" year="1987">
<Song title="With or Without You" peak="1" />
<Song title="I Still Haven't Found What I'm Looking For" peak="1" />
<Song title="Where The Streets Have No Name" peak="13" />
<Song title="In God's Country" peak="14" />
<Album title="Songs from the Big Chair" artist="Tears for Fears" year="1985">
<Song title="Shout" peak="1" />
<Song title="Everybody Wants to Rule the World" peak="1" />
<Song title="Head Over Heels" peak="3" />
<Song title="Mothers Talk" peak="27" />

The next code snippet shows some Groovy code using XMLSlurper to print out some details based on this source XML. The Groovy script in this case is called slurpXml.groovy.


#!/usr/bin/env groovy
// slurpXml.groovy
// Demonstrates use of Groovy's XML slurping.

albums = new XmlSlurper().parse("RockAndRoll.xml")

println "${it.@artist}'s album ${it.@title} was released in ${it.@year}."
println "\tFeaturing ${it.@title} that peaked in the U.S. at ${it.@peak}"

As the Groovy code above demonstrates, only a few lines of code are required to parse the XML and to print out its results as part of longer strings. The single line new XmlSlurper().parse("RockAndRoll.xml") is all it takes to parse the source XML. Then the variable to which those results are assigned (in this case, albums) provides access to the XML content via familiar syntax.

When the Groovy code above is executed, its results look like those shown in the following screen snapshot.

The Groovy User Guide has a section devoted to coverage of Reading XML Using Groovy's XmlSlurper. This section points out additional issues related to using Groovy's XmlSlurper such as dealing with XML tag names that include hyphens (use double quotes around name with hyphen included) and namespace matching details.


Because Groovy really is Java, Groovy can make use of the plethora of XML handling APIs for Java. However, Groovy can and does go beyond this and provides even easier-to-use APIs for XML manipulation. Groovy's XmlSlurper is an example of how Groovy makes XML reading/parsing/slurping easier than ever.

Additional References

Besides the Groovy User Guide section on XmlSlurper, there are many other online resources that cover use of XmlSlurper. I list some of them here.

Reading XML Using Groovy's XmlSlurper

Groovy: Processing Existing XML (6 March 2009)

Practically Groovy: Building, Parsing, and Slurping XML (19 May 2009)

Nothing Makes You Want Groovy More than XML (12 March 2008)

Updating XML with XmlSlurper

Groovy XMLSlurper

No comments: