xmlgroovymarkupbuilder

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?


Solution

  • 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