Open1

Groovy Beginner

みんなおやすみみんなおやすみ

Loop

def colors = ['red', 'green', 'blue']
colors.each { color ->
    println("color: ${color}")
}
def person = [name: 'John', age: 25]

person.each { key, value ->
    println("Key: ${key}, Value: ${value}")
}

Parsing XMLParser/XMLSlurper

def text = ${XMLData}
def list = new XmlParser().parseText(text)
list.technology.name.text() == 'Groovy'

Defferences two library

xmlSluperは遅延評価。別データに変換したい場合はsluper、update and readをしたい場合はparser

findAll

def parsed_XmlData = new XmlSlurper().parseText(XmlData)
def countData = resopnse.findAll {node ->
    node.name() == null
}*.text()

assert title.size() == 4

Builder

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()) 
assert records.car.first().name.text() == 'HSV Maloo'
assert records.car.last().name.text() == 'Royale'

もうこれ見て
https://groovy-lang.org/processing-xml.html#_parsing_xml