Groovy:GPath - 读取节点属性是映射的 xml 文件
我有以下 xml 文件:
<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>
此代码是使用 Groovy MarkupBuilder 创建的。
现在我想在 groovy 脚本中解析这个文件:
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
}
但我不断收到此异常:
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'
我的问题是,如何解析作为映射的 xml 属性?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑您发布的代码和 xml 不是您遇到问题的实际代码和 xml...
但是,假设您打算在发布示例 xml 时关闭
标记,我尝试了这个 Groovy 代码:它可以工作,并打印出:
这些是字符串,而不是您似乎想要的地图...
要将它们作为地图,您可以这样做:
您需要
tr
调用,将您为地图选择的格式转换为groovy可以处理的格式...正如您所说,您生成XML,我可以建议您将xml更改为:
然后,您可以跳过
tr( )
步骤...或者使用 Json 或除您的定制格式之外的其他格式?不确定将地图存储在属性中是一个好的方法...对我来说感觉有点脆弱:-/
编辑
我明白你的意思,它是Groovy MarkupBuilder 在添加属性作为 Map 时添加了奇怪的格式...
也许一个解决方案是执行类似的操作?
这将输出:
如您所见,每个映射条目都作为属性添加,并且可以使用每个
Node
类上的attributes()
调用将这些条目提取出来XmlParserI 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
tr
call, 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 eachNode
class from theXmlParser