4
我很難在xml結構中添加更深的節點。我缺少和node和nodeList之間的東西。任何幫助將不勝感激。Groovy節點與節點列表
def xml='''<Root id="example" version="1" archived="false">
<Item name="one" value="test"/>
<Item name="two" value="test2"/>
<Item name="three" value="test3"/>
<AppSettings Name="foo" Id="foo1">
<roles>foo</roles>
</AppSettings>
<AppSettings Name="bar" Id="bar1">
<Item name="blue" value=""/>
<Item name="green" value=""/>
<Item name="yellow" value=""/>
<Roles>
<Role id="A"/>
<Role id="B"/>
<Role id="C"/>
</Roles>
</AppSettings>
</Root>'''
root = new XmlParser().parseText(xml)
def appSettings = root.'AppSettings'.find{[email protected] == "bar"}.'Roles'
appSettings.appendNode('Role', [id: 'D'])
def writer = new StringWriter()
def printer = new XmlNodePrinter(new PrintWriter(writer))
printer.preserveWhitespace = true
printer.print(root)
String result = writer.toString()
println result
錯誤
groovy.lang.MissingMethodException: No signature of method: groovy.util.NodeList.appendNode() is applicable for argument types: (java.lang.String, java.util.LinkedHashMap) values: [Role, [id:D]]
太棒了!謝謝Tim,我知道我錯過了一些簡單的東西。我希望我可以多次投票,因爲在研究過程中我找不到任何東西。再次感謝。 – zuichuan 2012-08-09 13:19:13