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

// require(groupId:'xmlunit', artifactId:'xmlunit', version:'1.0')
// require(groupId:'dom4j', artifactId:'dom4j', version:'1.6.1')
import javax.xml.parsers.DocumentBuilderFactory
import org.custommonkey.xmlunit.*
import org.dom4j.*

def addCar(root, name, make, year, country, type, text) {
    def car = root.addElement('car')
    car.addAttribute('name', name)
    car.addAttribute('make', make)
    car.addAttribute('year', year)
    def countryNode = car.addElement('country').addText(country)
    def record = car.addElement('record').addText(text)
    record.addAttribute('type', type)

def document = DocumentHelper.createDocument()
def root     = document.addElement('records')

addCar(root, 'HSV Maloo', 'Holden', '2006', 'Australia',
        'speed', 'Production Pickup Truck with speed of 271kph')
addCar(root, 'P50', 'Peel', '1962', 'Isle of Man',
        'size', 'Smallest Street-Legal Car at 99cm wide and 59 kg in weight')
addCar(root, 'Royale', 'Bugatti', '1931', 'France',
        'price', 'Most Valuable Car at $15 million')

// convert resulting document to a string so that we can compare
def writer = new StringWriter()
new XMLWriter(writer).writeNode(document)
def xmlDiff = new Diff(writer.toString(), XmlExamples.CAR_RECORDS)
assert xmlDiff.similar()

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:

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