Skip to: Site menu | Main content

Groovy 

      Download | Documentation | Developers | Community

An agile dynamic language for the Java Platform

Creating XML using Groovy's StreamingMarkupBuilder Add comment to Wiki View in Wiki Edit Wiki page Printable Version

Here is an example of using StreamingMarkupBuilder to create a new XML file:

// require(groupId:'xmlunit', artifactId:'xmlunit', version:'1.1beta2')
import groovy.xml.StreamingMarkupBuilder
import org.custommonkey.xmlunit.*

def xml = new StreamingMarkupBuilder().bind{
  records {
    car(name:'HSV Maloo', make:'Holden', year:2006) {
      country('Australia')
      record(type:'speed', 'Production Pickup Truck with speed of 271kph')
    }
    car(name:'P50', make:'Peel', year:1962) {
      country('Isle of Man')
      record(type:'size', 'Smallest Street-Legal Car at 99cm wide and 59 kg in weight')
    }
    car(name:'Royale', make:'Bugatti', year:1931) {
      country('France')
      record(type:'price', 'Most Valuable Car at $15 million')
} } }

XMLUnit.ignoreWhitespace = true
def xmlDiff = new Diff(xml.toString(), XmlExamples.CAR_RECORDS)
assert xmlDiff.similar()

Note that StreamingMarkupBuilder.bind returns a Writable instance that may be used to stream the markup to a Writer, in addition to capturing the output in a String (as shown above).

We have used XMLUnit to compare the XML we created with our sample XML. To do this, make sure the sample XML is available, i.e. that the following class is added to your CLASSPATH:

XmlExamples.groovy
class XmlExamples {
  static def CAR_RECORDS = '''
    <records>
      <car name='HSV Maloo' make='Holden' year='2006'>
        <country>Australia</country>
        <record type='speed'>Production Pickup Truck with speed of 271kph</record>
      </car>
      <car name='P50' make='Peel' year='1962'>
        <country>Isle of Man</country>
        <record type='size'>Smallest Street-Legal Car at 99cm wide and 59 kg in weight</record>
      </car>
      <car name='Royale' make='Bugatti' year='1931'>
        <country>France</country>
        <record type='price'>Most Valuable Car at $15 million</record>
      </car>
    </records>
  '''
}

Here's another example illustrating how to produce mixed content:

// require(groupId:'xmlunit', artifactId:'xmlunit', version:'1.1beta2')
import groovy.xml.StreamingMarkupBuilder
import org.custommonkey.xmlunit.*

def message = ' <b>wins</b> '
def xml = new StreamingMarkupBuilder().bind{
  html() {
    body(bgcolor:'red') {
      h1('In Breaking News ...')
      p {
        a(href:'http://groovy.codehaus.org', 'Groovy')
        mkp.yieldUnescaped message
        a(href:'http://jax-award.de/jax_award/gewinner_eng.php', 'Jax')
        mkp.yield ' praise & award.'
} } } }

def expected = '''
<html>
    <body bgcolor='red'>
        <h1>In Breaking News ...</h1>
        <p>
            <a href='http://groovy.codehaus.org'>Groovy</a>
            <b>wins</b>
            <a href='http://jax-award.de/jax_award/gewinner_eng.php'>Jax</a>
            praise &amp; award.
        </p>
    </body>
</html>
'''

XMLUnit.ignoreWhitespace = true
def xmlDiff = new Diff(xml.toString(), expected)
assert xmlDiff.similar()

You may also want to see Using MarkupBuilder for Agile XML creation.