Using XmlSlurper/GPathResult to parse a simple XML file.
import groovy.transform.ToString
/*
<?xml version="1.0" encoding="UTF-8" ?>
<records>
<record>
<first>Evelyn</first>
<last>Vinson</last>
<age>54</age>
</record>
...
</records>
*/
@ToString
class Person {
// Annotation will format a nice default toString for us.
def first, last, age
}
def people = [ ]
// GPathResult: Something like XPath for nested data.
def records = new XmlSlurper().parse('records.xml')
records.record.each {
def p = new Person()
p.first = it.first.text()
p.last = it.last.text()
p.age = it.age.toInteger()
people.add(p)
}
people.each {
// Prints toString for each record: Person(Evelyn, Vinson, 54)
println it
}