The following simple XML code will be used to demonstrate Groovy's XmlSlurper. The XML file for this example is called
RockAndRoll.xml
.RockAndRoll.xml
<Albums>
<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>
<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>
<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>
<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" />
</Album>
</Albums>
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
.slurpXml.groovy
#!/usr/bin/env groovy
// slurpXml.groovy
// Demonstrates use of Groovy's XML slurping.
//
albums = new XmlSlurper().parse("RockAndRoll.xml")
albums.Album.each
{
println "${it.@artist}'s album ${it.@title} was released in ${it.@year}."
it.Song.each
{
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.
Conclusion
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:
Post a Comment