This example assumes the following class is already on your CLASSPATH:
Here is an example of using JDOM with Groovy to process an existing XML file:
// require(groupId:'jdom', artifactId:'jdom', version:'1.0')
import org.jdom.input.SAXBuilder
def reader = new StringReader(XmlExamples.CAR_RECORDS)
def records = new SAXBuilder().build(reader).rootElement
def messages = []
records.children.iterator().each{ car ->
def make = car.getAttribute('make').value
def country = car.getChildText('country')
def type = car.getChild('record').getAttribute('type').value
messages << make + ' of ' + country + ' has a ' + type + ' record'
}
assert messages == [
'Holden of Australia has a speed record',
'Peel of Isle of Man has a size record',
'Bugatti of France has a price record'
]
|