Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Here is an example of using XOM with Groovy to process an existing XML file:

Code Block
// requires xom.jarrequire(groupId:'xom', artifactId:'xom', version:'1.1')
import nu.xom.Builder

def reader   = new StringReader(XmlExamples.CAR_RECORDS)
def records  = new Builder().build(reader).rootElement
messages = []

def processCar(car) {
    def make = car.getAttribute('make').value
    def country = car.getFirstChildElement('country').value
    def type = car.getFirstChildElement('record').getAttribute('type').value
    messages << make + ' of ' + country + ' has a ' + type + ' record'
}

def cars = records.childElements
(0..<cars.size()).each{ processCar(cars.get(it)) }

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'
]