开发者

Parse XML file in groovy with "-" in node name

开发者 https://www.devze.com 2023-03-26 09:50 出处:网络
if i parse this xml file: <?xml version=\"1.0\" encoding=\"UTF-8\"?> <rootnode> &l开发者_运维知识库t;testnode info=\"Test1\"/>

if i parse this xml file:

<?xml version="1.0" encoding="UTF-8"?>
<rootnode>
  &l开发者_运维知识库t;testnode info="Test1"/>
  <testnode info="Test2"/>
</rootnode>

with this pice of groovy code all works fine:

def parser = new XmlParser()
def result = parser.parse(new File('test.xml').getCanonicalPath())
result.testnode.each {node -> println node.'@info'}

But if the node name change from "testnode" to "test-node"

i got the following message:

Exception in thread "main" groovy.lang.MissingPropertyException: No such property: node for class:..

How can i solve this problem (the content of the xml is untouchable)

Thanks!


You'll need to quote the nodename so Groovy knows it's not a subtraction:

result.'test-node'.each {node -> println node.'@info'}
0

精彩评论

暂无评论...
验证码 换一张
取 消