I have following xml file:
<doc_xml>
<nodes>
<node id='1' spec="{spec_a=0.9, spec_b=0.1}" />
<node id='2' spec="{spec_a=0.1, spec_b=0.3}" />
<node id='3' spec="{}" />
</nodes>
</doc_xml>
This code was created using Groovy MarkupBuilder.
Now I would like to parse this file in a groovy script:
def xml = new XmlParser().parseText(getDocXmlAsString());
xml.nodes.node.each {
Map spec = it.@spec; // here I got an exception org.codehaus.groovy.runtime.typehandling.GroovyCastException
}
but I keep getting this exception:
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '{spec_a=0.9, spec_b=0.1}' with class 'java.lang.String' to class 'java.util.Map'
My question, how to parse an xml attribute which is a map?
I suspect the code and xml you posted are not the actual code and xml you are having troubles with…
However, assuming you meant to close the
<node/>tags when posting your example xml, I tried this Groovy code:And it works, and prints out:
These are Strings, not Maps as you seem to want…
To get them as Maps, you could do:
You need the
trcall, to convert the format you have chosen for your maps into a format groovy can handle…As you say you generate the XML, can I suggest you change your xml to:
As then, you can skip the
tr()step…or use Json or something other than your bespoke format?Not sure storing a map in an attribute is a good way forward…it feels a little bit brittle to me :-/
EDIT
I see what you mean, it is the Groovy MarkupBuilder adding that strange formating when it adds an attribute as a Map…
Maybe one solution would be to do something like this?
That would output:
As you can see, each map entry is added as an attribute, and these can be extracted back out using the
attributes()call on eachNodeclass from theXmlParser