Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy XML MarkupBuilder does not create root <?xml .... ?>

Tags:

xml

grails

groovy

I am using XML MarkupBuilder to generate XML files

import groovy.xml.StreamingMarkupBuilder
import groovy.xml.MarkupBuilder
import org.custommonkey.xmlunit.*

....

def xmlObj = new StringWriter()
def xml = new MarkupBuilder(xmlObj)
xml.book("id":21){
    name('bookname')
}

and I get the output:

<book id='21'>
    <name>bookname</name>
</book>

how can I make it generate the root xml tag as well?

<?xml version="1.0" encoding="utf-8"?>
like image 449
iMiX Avatar asked May 27 '13 17:05

iMiX


2 Answers

You need to add it yourlsef

def xml = new StreamingMarkupBuilder().bind{
        mkp.xmlDeclaration()
        task {
            id(_Id)
            name("clean")
        }
    }
like image 36
moskiteau Avatar answered Oct 14 '22 12:10

moskiteau


You can add xml declaration with MarkupBuilder too:

def xml = new MarkupBuilder(xmlObj)
xml.mkp.xmlDeclaration(version: "1.0", encoding: "utf-8")
xml.book("id":21){
    name('bookname')
}
like image 141
Sergei Shushkevich Avatar answered Oct 14 '22 13:10

Sergei Shushkevich