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.

dmahapatro
  • 49,365
  • 7
  • 88
  • 117
saravana_pc
  • 2,607
  • 11
  • 42
  • 66

1 Answers1

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()
tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • Wow, great. The documentation for XMLSlurper lacks a lot: http://groovy.codehaus.org/Reading+XML+using+Groovy's+XmlSlurper – saravana_pc Apr 30 '14 at 15:19
  • Same is applicable if `XmlParser` is used. – dmahapatro Apr 30 '14 at 15:20
  • @saravana_pc You should be looking at [GPathResult](http://groovy.codehaus.org/api/groovy/util/slurpersupport/GPathResult.html) Api or [Node](http://groovy.codehaus.org/api/groovy/util/Node.html) Api instead, because the parsed result is one of those when XmlSlurper ot XmlParser is used respectively. – dmahapatro Apr 30 '14 at 15:22
  • In this case, it's a [NodeChild](http://groovy.codehaus.org/api/groovy/util/slurpersupport/NodeChild.html) -- `assert new XmlSlurper().parseText( xml ).getClass().name == 'groovy.util.slurpersupport.NodeChild'` – tim_yates Apr 30 '14 at 15:25