Groovy - XmlSlurper - read attributes into a Map
Asked Answered
D

1

6
<racebet amount="8.89" id="6852465" bettype="K" instance="1" type="csf" />

What is the best way to create a map containing the attributes as keys and the corresponding values?

thanks.

Davita answered 30/4, 2014 at 15:10 Comment(0)
S
7

Given:

def xml = '<racebet amount="8.89" id="6852465" bettype="K" instance="1" type="csf" />'

You can simply do:

def attrmap = new XmlSlurper().parseText( xml ).attributes()
Stop answered 30/4, 2014 at 15:12 Comment(4)
Wow, great. The documentation for XMLSlurper lacks a lot: groovy.codehaus.org/Reading+XML+using+Groovy's+XmlSlurperDavita
Same is applicable if XmlParser is used.Farrel
@Davita You should be looking at GPathResult Api or Node Api instead, because the parsed result is one of those when XmlSlurper ot XmlParser is used respectively.Farrel
In this case, it's a NodeChild -- assert new XmlSlurper().parseText( xml ).getClass().name == 'groovy.util.slurpersupport.NodeChild'Stop

© 2022 - 2024 — McMap. All rights reserved.