14

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

jabal
  • 11,987
  • 12
  • 51
  • 99

2 Answers2

22

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

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
tim_yates
  • 167,322
  • 27
  • 342
  • 338
1

Try MarkupBuilderHelper.comment. The linked page has an example.

Uses getMkp() to get the MarkupBuilderHelper.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
LarsH
  • 27,481
  • 8
  • 94
  • 152