Groovy

Parsing XML is fun again- thanks to Groovy

I happened to work on Groovy while I was developing using Gaelyk and I found Groovy lot easier to write- I never thought of the types and this really helped me to make my codeGroov simpler and easier to write. And when you are dealing with RESTful APIs- you ought to be parsing XML,JSON and what not. As a Java programmer- I never enjoyed it. But Groovy really made parsing xml fun again. Let me show you a sample-

def xml = """<students>
  <student>
    <name>Raj</name>
    <age>16</age>
    <school>Kendriya Vidyalaya</school>
    <subject>English</subject>
    <subject>Physics</subject>
    <subject>Chemistry</subject>
    <subject>Math</subject>
  </student>
  <student>
    <name>Mohan</name>
    <age>17</age>
    <school>Navodaya Vidyalaya</school>
    <subject>English</subject>
    <subject>Physics</subject>
    <subject>Chemistry</subject>
    <subject>Computer Science</subject>
  </student>
  <student>
    <name>Kiran</name>
    <age>16</age>
    <school>National Public School</school>
    <subject>English</subject>
    <subject>Physics</subject>
    <subject>Chemistry</subject>
    <subject>Biology</subject>
  </student>
</students>
"""
def students = new XmlSlurper().parseText(xml)
students.student.each{ student ->
   print("Name: ${student.name}")
   print("Age: ${student.age}")
   println("School: ${student.school}")
   print("Subjects:")
   student.subject.each{subject ->
      print("${subject}")
   }
   println("")
}

Most of the lines of code above is for creating the sample XML, if we actually see the code parsing the XML- its 10 lines (with a bit of jazz for the output).

[bash]

C:>groovy xmlparser.groovy
Name: Raj Age: 16 School: Kendriya Vidyalaya
Subjects: English Physics Chemistry Math
Name: Mohan Age: 17 School: Navodaya Vidyalaya
Subjects: English Physics Chemistry Computer Science
Name: Kiran Age: 16 School: National Public school
Subjects: English Physics Chemistry Biology

[/bash]

And one can see the output above.

The code is pretty self explanatory- We are using the XmlSlurper utility class in the groovy.util package for parsing the XML. The way the XML elements are accessed is somewhat similar to the Xpath notation, just that the “/” is replaced by “.”. One can access the attributes of the xml node by using- node.@attributeName.

Also if the element is missing in the XML and you try to access it- It doesnt throw a NullPointerException, instead it silently prints empty string- This is something I really liked- I need not worry too much about element being present or not.

For more reading-Groovy Recipes: Greasing The Wheels Of Java

Advertisements

Categories: Groovy

Tagged as: , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s