如何使用XmlSlurper删除Groovy中的元素?

前端之家收集整理的这篇文章主要介绍了如何使用XmlSlurper删除Groovy中的元素?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
例如,如何以编程方式删除rootNode中名称为1的所有标记

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 )

猜你在找的Groovy相关文章