如何使用 XmlSlurper 删除 Groovy 中的元素? [英] How to remove an element in Groovy using XmlSlurper?
本文介绍了如何使用 XmlSlurper 删除 Groovy 中的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,如何以编程方式删除 rootNode
中名称为 one
的所有标签?
For example, how can I remove all tags with name one
in rootNode
programmatically?
def rootNode = new XmlSlurper().parseText(
'<root><one a1="uno!"/><two>Some text!</two></root>' )
我试过了
rootNode.children().removeAll{ it.name() == 'one' }
但它报告:
groovy.lang.MissingMethodException: No signature of method: groovy.util.slurpersupport.NodeChildren.removeAll() is applicable for argument types: (DUMMY$_closure1_closure2) values: [DUMMY$_closure1_closure2@6c5f92d3]
推荐答案
尝试
rootNode.one.replaceNode { }
完成答案:
def rootNode = new XmlSlurper().parseText (
'<root><one a1="uno!"/><two>Some text!</two></root>'
)
rootNode.one.replaceNode { }
println groovy.xml.XmlUtil.serialize( rootNode )
这篇关于如何使用 XmlSlurper 删除 Groovy 中的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文