Serializing Groovy Beans to XML
In this recipe, we are going to learn how to serialize a Groovy Bean into XML and back. Groovy Beans are discussed in detail in the Writing less verbose Java Beans with Groovy Beans recipe from Chapter 3, Using Groovy Language Features. The steps from this recipe can be applied either to POJO or POGO.
Getting ready
Groovy doesn't have a default XML object serializer. The groovy.xml.MarkupBuilder
is an excellent tool for generating XML in a fluent way but doesn't offer any simple mechanism to create an XML document out of bean properties.
There is, on the other hand, a large offer of third-party Java libraries for XML serialization. In this recipe, we are going to look at XStream (http://xstream.codehaus.org/). XStream is a very popular library, with frequent releases and a dead-simple API. Did we mention XStream is also fast and has a ridiculously low memory footprint?
How to do it...
The following steps offer an insight into how to achieve our task:
Before we can...