Here is an example of using
MarkupBuilder to create a new XML file:
This will output XML similar to what we have been using in previous examples.
You may also want to see Using MarkupBuilder for Agile XML creation.
If we want we can take this further and compare what we produced with our sample XML. To do this, make sure the following class is added to your CLASSPATH:
We can now extend the previous example to compare the newly created XML with our sample XML using XMLUnit:
We have a little bit of work to do. By default XMLUnit allows elements and attributes to be reordered and still treats two XML documents as similar but by default it doesn't allow differences in whitespace; hence the need for
IgnoreWhitespaceDifferenceListener as shown below:
As a final example, suppose we have an existing XML document and we want to automate generation of the markup without having to type it all in? We just need to use
DomToGroovy as shown in the following example:
Running this will produce the builder code for us.