Tuesday, October 6, 2009

Getting Groovy with XML Parsing

I generally like to use Groovy for data processing, particularly XML parsing. If you've used Groovy before, this won't be anything new, but if you haven't, this example might be enough to make you drink the groovy juice. Given this xml file of an address book, I want to print out all of the people in the address book.

<addressBook>
<person>
<firstName>John</firstName>
<lastName>Smith</lastName>
</person>
<person>
<firstName>Jane</firstName>
<lastName>Doe</lastName>
</person>
</addressBook>


Here's the Java way of doing it (if you've ever done any Java XML parsing, this will look very familiar):

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder parser = factory.newDocumentBuilder();
Document document = parser.parse("xml/sample.xml");
NodeList personNodes = document.getElementsByTagName("person");
List<String> names = new LinkedList<String>();
for (int i = 0; i < personNodes.getLength(); i++) {
String firstName = null;
String lastName = null;
Node personNode = personNodes.item(i);
NodeList children = personNode.getChildNodes();
for (int j = 0; j < children.getLength(); j++) {
Node child = children.item(j);
String nodeName = child.getNodeName();
String nodeValue = child.getTextContent();
if ("firstName".equals(nodeName)) {
firstName = nodeValue;
} else if ("lastName".equals(nodeName)) {
lastName = nodeValue;
}
}
names.add(firstName + " " + lastName);
}
for (String name : names) {
System.out.println(name);
}


That's 25 lines of Java code to print out a few names (and I didn't even include the public static void main(String[] args) declaration).

Now let's look at the groovy way of doing it:

def addressBook = new XmlSlurper().parse('xml/sample.xml')
addressBook.person.each { p -> println "${p.firstName.text()} ${p.lastName.text()}"}

Just 2 lines, that's it.

Happy groovy-ing.

No comments:

Post a Comment