如何使用 Groovy 在 xml 中插入/移动/删除节点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/224926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to insert/move/delete nodes in xml with Groovy?
提问by flyisland
for example, I have the following xml document:
例如,我有以下 xml 文档:
def CAR_RECORDS = '''
<records>
<car name='HSV Maloo' make='Holden' year='2006'/>
<car name='P50' make='Peel' year='1962'/>
<car name='Royale' make='Bugatti' year='1931'/>
</records>
'''
and I want to move the car "Royale" up to first one, and insert a new car just after car"HSV Maloo", the result would be:
我想将汽车“Royale”移到第一个,并在汽车“HSV Maloo”之后插入一辆新车,结果将是:
'''
<records>
<car name='Royale' make='Bugatti' year='1931'/>
<car name='HSV Maloo' make='Holden' year='2006'/>
<car name='My New Car' make='Peel' year='1962'/>
<car name='P50' make='Peel' year='1962'/>
</records>
'''
How to do it with Groovy? comments are welcome.
如何用 Groovy 做到这一点?欢迎提出意见。
采纳答案by Ted Naleid
I went down a similar route to danb, but ran into problems when actually printing out the resulting XML. Then I realized that the NodeList that was returned by asking the root for all of it's "car" children isn't the same list as you get by just asking for the root's children. Even though they happen to be the same lists in this case, they wouldn't always be if there were non "car" children under the root. Because of this, reording the list of cars that come back from the query doesn't affect the initial list.
我沿着与 danb 类似的路线走下去,但在实际打印结果 XML 时遇到了问题。然后我意识到通过向根询问所有“汽车”子级而返回的 NodeList 与仅询问根的子级而获得的列表不同。即使在这种情况下它们碰巧是相同的列表,但如果根下有非“汽车”子项,它们也不会总是如此。因此,重新排列从查询返回的汽车列表不会影响初始列表。
Here's a solution that appends and reorders:
这是一个附加和重新排序的解决方案:
def CAR_RECORDS = '''
<records>
<car name='HSV Maloo' make='Holden' year='2006'/>
<car name='P50' make='Peel' year='1962'/>
<car name='Royale' make='Bugatti' year='1931'/>
</records>
'''
def carRecords = new XmlParser().parseText(CAR_RECORDS)
def cars = carRecords.children()
def royale = cars.find { it.@name == 'Royale' }
cars.remove(royale)
cars.add(0, royale)
def newCar = new Node(carRecords, 'car', [name:'My New Car', make:'Peel', year:'1962'])
assert ["Royale", "HSV Maloo", "P50", "My New Car"] == carRecords.car*.@name
new XmlNodePrinter().print(carRecords)
The assertion with the propertly ordered cars passes, and the XmlNodePrinter outputs:
具有正确排序的汽车的断言通过,并且 XmlNodePrinter 输出:
<records>
<car year="1931" make="Bugatti" name="Royale"/>
<car year="2006" make="Holden" name="HSV Maloo"/>
<car year="1962" make="Peel" name="P50"/>
<car name="My New Car" make="Peel" year="1962"/>
</records>
回答by flyisland
ted, maybe you did not notice that I wanted to '''insert a new car just after car"HSV Maloo"''', so I modify your code to :
ted,也许您没有注意到我想'''在汽车“HSV Maloo”之后插入一辆新车''',所以我将您的代码修改为:
def newCar = new Node(null, 'car', [name:'My New Car', make:'Peel', year:'1962'])
cars.add(2, newCar)
new XmlNodePrinter().print(carRecords)
now, it works with proper order! thanks to danb & ted.
现在,它以正确的顺序工作!感谢 danb 和 ted。
<records>
<car year="1931" make="Bugatti" name="Royale"/>
<car year="2006" make="Holden" name="HSV Maloo"/>
<car name="My New Car" make="Peel" year="1962"/>
<car year="1962" make="Peel" name="P50"/>
</records>
回答by danb
<hand-wave> these are not the codz you seek</hand-wave>
<hand-wave> 这些不是你想要的代码</hand-wave>
Node root = new XmlParser().parseText(CAR_RECORDS)
NodeList carNodes = root.car
Node royale = carNodes[2]
carNodes.remove(royale)
carNodes.add(0, royale)
carNodes.add(2, new Node(root, 'car', [name:'My New Card', make:'Peel', year:'1962']))
I don't know if there's a smarter way to create new nodes... but that works for me.
我不知道是否有更聪明的方法来创建新节点……但这对我有用。
EDIT: uhg... thanks guys... I got lazy and was printing carNodes when i tested this instead of the root... yikes.
编辑:呃......谢谢大家......当我测试这个而不是根时,我变得懒惰并且正在打印carNodes......哎呀。

