例如,如何以编程方式删除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 )