Groovy:GPath - 读取节点属性是映射的 xml 文件

发布于 2024-09-26 12:07:35 字数 788 浏览 3 评论 0原文

我有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北音执念 2024-10-03 12:07:35

我怀疑您发布的代码和 xml 不是您遇到问题的实际代码和 xml...

但是,假设您打算在发布示例 xml 时关闭 标记,我尝试了这个 Groovy 代码:

def xmlStr = """<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>"""

def xml = new XmlParser().parseText( xmlStr )
def spec = [:]

xml.nodes.node.each {
  spec = it.@spec
  println spec
}

它可以工作,并打印出:

{spec_a=0.9, spec_b=0.1}
{spec_a=0.1, spec_b=0.3}
{}

这些是字符串,而不是您似乎想要的地图...

要将它们作为地图,您可以这样做:

xml.nodes.node.each {
  spec = Eval.me( [email protected]( '{=}', '[:]' ) )
  println spec
}

您需要 tr调用,将您为地图选择的格式转换为groovy可以处理的格式...

正如您所说,您生成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>

然后,您可以跳过 tr( ) 步骤...或者使用 Json 或除您的定制格式之外的其他格式?

不确定将地图存储在属性中是一个好的方法...对我来说感觉有点脆弱:-/

编辑

我明白你的意思,它是Groovy MarkupBuilder 在添加属性作为 Map 时添加了奇怪的格式...

也许一个解决方案是执行类似的操作?

import groovy.xml.MarkupBuilder

// Imaginary data that we're going to generate our XML from:
def nodeData = [
  [ id:1, spec_a:0.9, spec_b:0.1 ],
  [ id:2, spec_a:0.1, spec_b:0.3 ],
  [ id:3 ]
]

def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.doc_xml() {
  nodes() {
    nodeData.each {
      node( it )
    }
  }
}

def xmlStr = writer.toString()

println "Write out the XML"
println xmlStr

def xmlParse = new XmlParser().parseText( xmlStr )
def spec = [:]

println "Write out the Attributes for each node"
xmlParse.nodes.node.each {
  spec = it.attributes()
  println spec
}

这将输出:

Write out the XML
<doc_xml>
  <nodes>
    <node id='1' spec_a='0.9' spec_b='0.1' />
    <node id='2' spec_a='0.1' spec_b='0.3' />
    <node id='3' />
  </nodes>
</doc_xml>
Write out the Attributes for each node
[id:1, spec_a:0.9, spec_b:0.1]
[id:2, spec_a:0.1, spec_b:0.3]
[id:3]

如您所见,每个映射条目都作为属性添加,并且可以使用每个 Node 类上的 attributes() 调用将这些条目提取出来XmlParser

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:

def xmlStr = """<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>"""

def xml = new XmlParser().parseText( xmlStr )
def spec = [:]

xml.nodes.node.each {
  spec = it.@spec
  println spec
}

And it works, and prints out:

{spec_a=0.9, spec_b=0.1}
{spec_a=0.1, spec_b=0.3}
{}

These are Strings, not Maps as you seem to want...

To get them as Maps, you could do:

xml.nodes.node.each {
  spec = Eval.me( [email protected]( '{=}', '[:]' ) )
  println spec
}

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:

<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>

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?

import groovy.xml.MarkupBuilder

// Imaginary data that we're going to generate our XML from:
def nodeData = [
  [ id:1, spec_a:0.9, spec_b:0.1 ],
  [ id:2, spec_a:0.1, spec_b:0.3 ],
  [ id:3 ]
]

def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.doc_xml() {
  nodes() {
    nodeData.each {
      node( it )
    }
  }
}

def xmlStr = writer.toString()

println "Write out the XML"
println xmlStr

def xmlParse = new XmlParser().parseText( xmlStr )
def spec = [:]

println "Write out the Attributes for each node"
xmlParse.nodes.node.each {
  spec = it.attributes()
  println spec
}

That would output:

Write out the XML
<doc_xml>
  <nodes>
    <node id='1' spec_a='0.9' spec_b='0.1' />
    <node id='2' spec_a='0.1' spec_b='0.3' />
    <node id='3' />
  </nodes>
</doc_xml>
Write out the Attributes for each node
[id:1, spec_a:0.9, spec_b:0.1]
[id:2, spec_a:0.1, spec_b:0.3]
[id:3]

As you can see, each map entry is added as an attribute, and these can be extracted back out using the attributes() call on each Node class from the XmlParser

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文