本文介绍了如何使用groovy在xml中附加一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个xml,我想添加一个新的行
<行>
< row>
< name> Abhishek< / name>
< host> [email protected]< / host>
< / row>
< row>
< name> Simon< / name>
< host> [email protected]< / host>
< / row>
<行>
我使用MarkupBuilder(writer)来创建这个xml,但现在我怎么追加一个新的 已编辑: $ <$
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
$ b $为了在Groovy中做到这一点,一个解决方案是:
import groovy.xml.StreamingMarkupBuilder
def xml =< rows>
< row>
< name> Abhishek< / name>
< host> [email protected]< / host>
< / row>
< row>
< name> Simon< / name>
< host> [email protected]< / host>
< / row>
< / rows>
def root = new XmlSlurper()。parseText xml)
root.appendNode {
row {
name('tim')
host('[email protected]')
}
}
def outputBuilder = new StreamingMarkupBuilder()
String result = outputBuilder.bind {mkp.yield root}
I have this xml where i'd like to add a new row
<rows>
<row>
<name>Abhishek</name>
<host>[email protected]</host>
</row>
<row>
<name>Simon</name>
<host>[email protected]</host>
</row>
<rows>
I used MarkupBuilder(writer) to create this xml but now how can i append a new row
element
Edited:
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
解决方案
To do this in Groovy, one solution is:
import groovy.xml.StreamingMarkupBuilder
def xml = """<rows>
<row>
<name>Abhishek</name>
<host>[email protected]</host>
</row>
<row>
<name>Simon</name>
<host>[email protected]</host>
</row>
</rows>"""
def root = new XmlSlurper().parseText( xml )
root.appendNode {
row {
name( 'tim' )
host( '[email protected]' )
}
}
def outputBuilder = new StreamingMarkupBuilder()
String result = outputBuilder.bind{ mkp.yield root }
这篇关于如何使用groovy在xml中附加一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!