On my current project, I refactored hard-coded data to a simple XML file similar to the following:
<test> <parent code="A"> <child>a</child> <child>b</child> </parent> <parent code="B"> <child>b</child> <child>c</child> </parent> </test>
Currently, we use Jakarta Commons Digester to parse XML. For this example, the code looks something like this:
Digester d = new Digester();
d.push(new ArrayList());
d.addObjectCreate("test/parent", "test.Code");
d.addSetProperties("test/parent");
d.addSetNext("test/parent", "add", "java.lang.Object");
d.addObjectCreate("test/parent/child", "test.Code");
d.addSetNestedProperties("test/parent", "child", "code");
d.addSetNext("test/parent/child", "addChild", "test.Code");
List parent = (List) d.parse(new File("test.xml"));
If you’re already familiar with Digester, it may be apparent what’s going on. If not, you’ll have to do a little reading to determine what the Digester methods do.
So, I thought I’d simplify things with (gasp) Groovy:
import org.codehaus.groovy.sandbox.util.XmlSlurper
import test.Code
class XmlLoader {
def List getCodes() {
def list = new ArrayList()
def xml = new XmlSlurper().parse(new File("test.xml"))
xml.parent.each {
def parent = new Code("code":it["@code"])
it.child.each { parent.addChild(new Code("code":it)) }
list.add(parent)
}
return list
}
}
Now, even if you don’t know Groovy, I daresay it’s easier to read the Groovy code than the Digester code.
I foresee these issues when trying to convince the former tech lead (who thinks he still is the tech lead) the Groovy solution might be better:
- He already groks Digester, so he won’t understand the “easier to read” argument.
- He won’t like the import from a “sandbox” package.
- Groovy is in its infancy; Digester is time-tested.
So, I’ll write a Digester solution just to be prepared, but (woe is me) it’s tough wanting to be on the cutting edge.

