例如,如何以编程方式删除one
中名称为rootNode
的所有代码?
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]
答案 0 :(得分:3)
尝试
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 )
答案 1 :(得分:0)
import groovy.xml.*
String xml = '<root><one a1="uno!"/><two>Some text!</two></root>'
def root = new XmlSlurper().parseText(xml)
root.one.replaceNode{}
def newRoot = new StreamingMarkupBuilder().bind {
mkp.yield root
}.toString()
println xml
println newRoot
输出:
<root><one a1="uno!"/><two>Some text!</two></root>
<root><two>Some text!</two></root>