Groovy:XmlSlurper,未能先替换节点,然后再次尝试追加相同的节点。任何人都面临这个问题
//无法使用xml slurper将替换的节点添加回xml。引发堆栈溢出异常。现在想想怎么做Groovy:XmlSlurper,未能先替换节点,然后再次尝试追加相同的节点。任何人都面临这个问题,groovy,xmlslurper,Groovy,Xmlslurper,//无法使用xml slurper将替换的节点添加回xml。引发堆栈溢出异常。现在想想怎么做 def xml = """<container> <listofthings> <thing id="100" name="foo"/> </listofthings> </container&g
def xml = """<container>
<listofthings>
<thing id="100" name="foo"/>
</listofthings>
</container>"""
def root = new XmlSlurper().parseText(xml)
def fun = new ArrayList(root.listofthings.collect{it})
root.listofthings.thing.each {
it.replaceNode {}
}
root.listofthings.appendNode ( { thing(id:102, name:'baz') })
fun.each {
root.listofthings.appendNode it
}
def outputBuilder = new groovy.xml.StreamingMarkupBuilder()
String result = outputBuilder.bind { mkp.yield root }
print result
defxml=”“”
"""
def root=new XmlSlurper().parseText(xml)
def fun=newarraylist(root.listofthings.collect{it})
根。事物列表。事物。每个{
it.replaceNode{}
}
root.listofthings.appendNode({thing(id:102,名称:'baz')})
每个人都很有趣{
root.listofthings.appendNode将其删除
}
def outputBuilder=new groovy.xml.StreamingMarkupBuilder()
字符串结果=outputBuilder.bind{mkp.yield root}
打印结果
您呼叫:
def fun = new ArrayList(root.listofthings.collect{it})
它将fun
设置为节点
(顺便说一句,可以缩短为:def fun=root.listofthings
)
然后在网上:
fun.each {
root.listofthings.appendNode it
}
将此节点附加到节点
。这意味着您的树将永远不会结束(因为您正在将一个节点附加到自身),因此会出现StackOverflowException
要运行代码,可以将其更改为:
import groovy.xml.StreamingMarkupBuilder
def xml = """<container>
| <listofthings>
| <thing id="100" name="foo"/>
| </listofthings>
|</container>""".stripMargin()
def root = new XmlSlurper().parseText(xml)
root.listofthings.thing*.replaceNode {}
root.listofthings.appendNode {
thing( id:102, name:'baz' )
}
def outputBuilder = new StreamingMarkupBuilder()
String result = outputBuilder.bind { mkp.yield root }
print result
import groovy.xml.StreamingMarkupBuilder
def xml=“”
|
|
|
|“.stripMargin()
def root=new XmlSlurper().parseText(xml)
root.listofthings.thing*.replaceNode{}
root.listofthings.appendNode{
东西(id:102,名字:'baz')
}
def outputBuilder=新StreamingMarkupBuilder()
字符串结果=outputBuilder.bind{mkp.yield root}
打印结果
ie:去掉递归节点添加
然而,我不确定你想用递归加法做什么,所以这可能不符合你的要求。。。你能解释一下你想看到什么样的结果吗
编辑
我设法让XmlParser做了我认为您想做的事情
def xml = """<container>
| <listofthings>
| <thing id="100" name="foo"/>
| </listofthings>
|</container>""".stripMargin()
def root = new XmlParser().parseText(xml)
def listofthings = root.find { it.name() == 'listofthings' }
def nodes = listofthings.findAll { it.name() == 'thing' }
listofthings.remove nodes
listofthings.appendNode( 'thing', [ id:102, name:'baz' ] )
nodes.each {
listofthings.appendNode( it.name(), it.attributes(), it.value() )
}
def writer = new StringWriter()
new XmlNodePrinter(new PrintWriter(writer)).print(root)
def result = writer.toString()
print result
defxml=”“”
|
|
|
|“.stripMargin()
def root=new XmlParser().parseText(xml)
def listofthings=root.find{it.name()=='listofthings'}
def nodes=listofthings.findAll{it.name()
listofthings.remove节点
appendNode('thing',[id:102,名称:'baz'])
每个节点{
appendNode(it.name()、it.attributes()、it.value())
}
def writer=new StringWriter()
新的XmlNodePrinter(新的PrintWriter(writer)).print(root)
def result=writer.toString()
打印结果
上面印着:
<container>
<listofthings>
<thing id="102" name="baz"/>
<thing id="100" name="foo"/>
</listofthings>
</container>
非常感谢你,蒂姆。我也可以使用XmlParser。我在玩XmlSlurper时发现,如果我们尝试添加已经被替换的节点,就会得到StackOverFlowException。我不知道它为什么会这样做?@siva在你的代码中,你正在从listofcodes
内部删除东西,然后将listofcodes
节点添加到自身。您尚未删除listofcodes
节点,因此最终会得到一个递归结构:-(