开发者

How to append a row in a xml using groovy

I have this xml where i'd like to add a new row

<rows>
  <row>
    <name>Abhishek</name>
    <host>abhishek123@cjb.net</host>
  </row>
  <row>
    <name>Simon</name>
    <host>a@a.com</host>
  </row>
<rows>

I used MarkupBuilder(writer) to create this xml but开发者_JAVA百科 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>abhishek123@cjb.net</host>
  </row>
  <row>
    <name>Simon</name>
    <host>a@a.com</host>
  </row>
</rows>"""

def root = new XmlSlurper().parseText( xml )
root.appendNode {
  row {
    name( 'tim' )
    host( 'a@woo.com' )
  }
}

def outputBuilder = new StreamingMarkupBuilder()
String result = outputBuilder.bind{ mkp.yield root }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜