17.1 Building XML

Most of us love to hate XML. Working with XML gets harder as the document size increases, and the tools and API support are not pleasant. My theory is that XML is like a human: it starts out cute when it’s small and gets annoying when it becomes bigger.

XML may be a fine format for machines to handle, but it’s rather unwieldy to work with directly. No one really wants to do it, but we’re forced to. Groovy alleviates this a great deal by making working with XML almost fun.

Let’s look at an example of creating XML documents in Groov using a builder:

UsingBuilders/UsingXMLBuilder.groovy
 
bldr = ​new​ groovy.xml.MarkupBuilder()
 
bldr.languages {
 
language(name: ​'C++'​) { author(​'Stroustrup'​)}
 
language(name: ​'Java'​) { ...

Get Programming Groovy 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.