Versions Compared

Key

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

...

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

Code Block
// requires jdom.jar
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'
]