XmlParser 返回一个节点,我不知道有什么方法可以在不转换为 xml 字符串的情况下将其插入标记或 StreamingMarkupBuilder。
import groovy.xml.*
def x = """
<root>
<somenode1>
<anode>foo</anode>
</somenode1>
<somenode2>
<anode>bar</anode>
</somenode2>
</root>
""".trim()
def otherXml = new XmlParser().parseText(x)
def writer = new StringWriter()
new XmlNodePrinter(new PrintWriter(writer)).print(otherXml.somenode1[0])
def builder = new MarkupBuilder()
builder.outermosttag( name: 'library') {
someothertag( name: 'shelf' ) {
mkp.yieldUnescaped writer.toString() //magically insert a nodelist of arbitrary XML from somewhere else
}
}
对于这类工作,我倾向于将 XmlSlurper 与 StreamingMarkupBuilder 一起使用:
import groovy.xml.*
def x = """
<root>
<somenode1>
<anode>foo</anode>
</somenode1>
<somenode2>
<anode>bar</anode>
</somenode2>
</root>
""".trim()
def otherXml = new XmlSlurper().parseText(x)
def builder = new StreamingMarkupBuilder().bind {
outermosttag( name: 'library') {
someothertag( name: 'shelf' ) {
mkp.yield otherXml.somenode1 //magically insert a nodelist of arbitrary XML from somewhere else
}
}
}