How do I read an XML file using XmlSlurper and insert in tree built with MarkupBuilder?
I'm writing a script that generates an XML document using MarkupBuilder and I'd like to insert an XML file in the tree read from a file. How do I do that? An example is below.
def writer = new StringWriter()
def builder = new MarkupBuilder(writer)
builder.root() {
new XmlSlurper().parse(new File("file.xml"))
}
I tried mkp.yield and mkp.yieldUnescaped but both result in escaped text appearing in the output. A post on the Groovy mailing list seems to suggest that this is possible with StreamingMarku开发者_Python百科pBuilder but it's not clear if a solution exists for MarkupBuilder.
See @Tim's answer for appending node to existing tree: How to append a row in a xml using groovy
Not tested, but this should get escaped output:
def(xml,build,output)
xml = new XmlSlurper().parseText( new File("file.xml").getText() )
build = new StreamingMarkupBuilder()
output = build.bind{ mkp.yieldUnescaped xml }
精彩评论