是否可以使用 Groovy's XMLSlurper并在索引处插入子节点?
目前 GPathResult 类有 putAt(index) 和 appendNode()。第一个替换索引处的元素而不是插入,第二个添加到末尾。
不幸的是,我依赖于 XmlSlurper 而不是 XmlParser。
谢谢。
最佳答案
找到了。如此简单(非常棒 Groovy)。只需使用闭包和 + 运算符在相关节点之后添加节点即可。
例如:
//Add the ac:MessageStatus after ac:MessageDateTime (this is an Acord message hence the ac:)
root.'ac:MessageDateTime' + {
'ac:MessageStatus' {
'ac:MessageStatusCode'('ac:Success')
'ac:SuccessCode'('ac:Success')
}
}
关于xml - Groovy XmlSlurper 和插入子节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5022353/