Assuming you are using XmlSlurper, try:
def audit = parsing.'event-details'.iterator().collectEntries {
[ it.name, it.value ]
}
Edit:
Ok, given the xml:
def x = '''<Event>
| <ipaddress>10.0.0.0</ipaddress>
| <sourcesystem>somwhere</sourcesystem>
| <username>fred91</username>
| <user>
| <username>fred91</username>
| <account>fredsaccount</account>
| <id>f2234232</id>
| </user>
| <event-details>
| <name>Prairie Farms</name>
| <value>Summer Lane Happy Land USA</value>
| </event-details>
| <event-details>
| <name>House on fire</name>
| <value>ashes</value>
| </event-details>
|</Event>'''.stripMargin()
And the Audit class:
@groovy.transform.ToString(includeNames=true)
class Audit {
String ipAddress, sourceSystem, username, account, id
Map eventDetails
}
You can do:
def audit = new XmlSlurper().parseText( x ).with { xml ->
new Audit( ipAddress : xml.ipaddress,
sourceSystem : xml.sourcesystem,
username : xml.username,
account : xml.user.account,
id : xml.user.id,
eventDetails : xml.'event-details'.iterator().collectEntries { [ it.name, it.value ] } )
}
println audit
to print:
Audit(ipAddress:10.0.0.0, sourceSystem:somwhere, username:fred91, account:fredsaccount, id:f2234232, eventDetails:[Prairie Farms:Summer Lane Happy Land USA, House on fire:ashes])
Note:
If you are using Groovy < 1.8.7, then change
eventDetails : xml.'event-details'.iterator().collectEntries { [ it.name, it.value ] } )
to
eventDetails : xml.'event-details'.inject( [:] ) { m, e -> m << [ (e.name): e.value ] } )