Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I insert an xml comment with Groovy MarkupBuilder?

I would like to insert comments into my xml document with a Groovy MarkupBuilder. How is it possible?

like image 847
jabal Avatar asked Oct 26 '10 11:10

jabal


1 Answers

You can use mkp.comment like so:

def writer = new StringWriter()
def builder = new groovy.xml.MarkupBuilder( writer )
builder.cars {
    mkp.comment "A comment"
    ford( type:'escort')
    ford( type:'fiesta')
 }

println writer

Which prints:

<cars><!-- A comment -->
  <ford type='escort' />
  <ford type='fiesta' />
</cars>

The mkp.XXX methods are described here

like image 91
tim_yates Avatar answered Sep 17 '22 12:09

tim_yates