Antilope
Antilope

Reputation: 445

groovy - access an attribute of the xml

I can't print name, where am I wrong? I can access the country field but this print does nothing..

import groovy.xml.*

def writer = new StringWriter()
def xml = new MarkupBuilder(writer) 

xml.records() { 
    car(name: 'HSV Maloo', make: 'Holden', year: 2006) {
        country('Australia')
        record(type: 'speed', 'Production Pickup Truck with speed of 271kph')
    }
    car(name: 'Royale', make: 'Bugatti', year: 1931) {
        country('France')
        record(type: 'price', 'Most Valuable Car at $15 million')
    }
}

def records = new XmlSlurper().parseText(writer.toString()) 

println(records.car.first().name.text())

Upvotes: 0

Views: 29

Answers (1)

Mark Bramnik
Mark Bramnik

Reputation: 42481

You should use @ to access the attribute of the XML tag. Assuming you want to print HSV Maloo, change the last line to:

println(records.car[0].@name)

Upvotes: 2

Related Questions