Parse XML file in groovy with "-" in node name
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'}
精彩评论