如何使用groovy将根元素添加到xml

时间:2014-04-02 20:51:40

标签: xml groovy

您好我想在

之前和之后添加根元素

<note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
</note>

<pad>
<note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
</note>
</pad>

1 个答案:

答案 0 :(得分:2)

一种方法是使用StreamingMarkupBuilder添加根节点。

def xml = """
            <note>
                <to>Tove</to>
                <from>Jani</from>
                <heading>Reminder</heading>
                <body>Don't forget me this weekend!</body>
            </note>
          """

def nodes = new XmlSlurper().parseText(xml)
def markup = new groovy.xml.StreamingMarkupBuilder()

println markup.bind {
    pad { 
        mkp.yield nodes 
    }
}