Light reading for XML with Groovy
Intuitive, naturally
Posted in Software & Security, 11th January 2008 01:01 GMT
Free whitepaper – Managing desktop software for fun and profit
Hands on, part 2 Groovy, unlike Java, provides a relatively streamlined way to read and write XML documents. In the first part of my two-piece tutorial, I looked at how you can use Groovy's flexible strings, closures and iterators to quickly create XML fragments and files.
The emphasis was on making the most of Groovy as a scripting language rather than on making use of the heavyweight XML parsers and libraries that it can access due to its use of the Java Virtual Machine (JVM) and ability to access Java libraries.
This time, I turn to the other side of the equation, and look at how you can use Groovy to quickly and simply process XML.
Groovy can be used with the full range of Java XML parsers and libraries - so in addition to vanilla DOM and SAX, you can use JDOM, XOM, StAX and the whole alphabet soup of alternatives that are available for the Java platform.
What we're interested in here, though, is using Groovy for the kind of ad-hoc scripting tasks that have traditionally been the preserve of languages such as Perl. Instead of the heavyweights we are going to focus on Groovy's XmlSlurper class, which provides a very natural and intuitive interface to XML.
Our example data is the following simple XML file:
<?xml version="1.0"?>
<people>
<person first_name='john' surname='smith'>
<age>37</age>
<gender>m</gender>
<kids count='2' />
</person>
<person first_name='jill' surname='jones'>
<age>28</age>
<gender>f</gender>
<kids count='0' />
</person>
<person first_name='clark' surname='kent'>
<age>22</age>
<gender>m</gender>
<kids count='3' />
</person>
</people>
The first task is to parse the file - which we've called pers.xml and have placed in a /groovy/progs directory - from disk and make it available for processing. In Groovy this is a one-liner:
def pers=new XmlSlurper().parse(new File("/groovy/progs/pers.xml"))
Once we've got our XmlSlurper object we can use dot notation to access elements and attributes within it. Using dot notation is a very natural way of working for Java developers, and helps to bridge the gap between object notation and hierarchic XML structure. The top-level element in this case is people, but we can find that simply enough:
println pers.name()
The above code will print people to the command-line or shell. Want to find out what the child elements are? Easy:
pers.children().each { println it.name() }
But why stop there? We can move up and down the tree easily enough. Say we want to find the children of each of the child elements in turn. Again, it's trivial:
pers.children().children().each {println it.name()}
That should print out three lots of age, gender and kids. Getting hold of attributes isn't much more difficult:
pers.children().each {println it.attributes()}
The output of that line of code is:
["first_name":"john", "surname":"smith"] ["first_name":"jill", "surname":"jones"] ["first_name":"clark", "surname":"kent"]
Note that the above is a map, with the attribute names acting as the keys.
Free whitepaper – Straight Talk with Dell: Sending out an SaaS
Analyst Keynote: The Register Agile Data Center Summit
Dell PowerEdge M710 with Dell EqualLogic storage vs. HP ProLiant BL685c with HP StorageWorks EVA 4400
Seven ways to optimize VMware server virtualization

Sign up, sign up for The Register IT security newsletter
Microsoft's Windows 7 price gamble - and why it's flawed
Managing Desktop Software for fun and profit
Intel's flash new SSDs hit by bugs