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:

  1. Before we can use XStream, we need to fetch the dependency using Grape and import it:
    @Grab('com.thoughtworks.xstream:xstream:1.4.3')
    import com.thoughtworks.xstream.*
    import com.thoughtworks.xstream.io.xml.*
  2. Then we continue by creating a couple of Groovy Beans representing a customer:
    import groovy.transform.TupleConstructor
    @TupleConstructor
    class Customer {
       Long id
       String name
       String lastName
       Address businessAddress
    }
    @TupleConstructor
    class Address {
        String street
        String postcode
        String city
        String country
    }

    The @TupleConstructor annotation is mentioned in the Writing less verbose Java Beans with Groovy Beans from Chapter 3, Using Groovy Language Features.

  3. Finally, let's get down to business and serialize our customer:
    def xstream = new XStream()
    def john = new Customer(
                 100,
                 'John',
                 'Red',
                  new Address(
                    'Ocean Drive 101',
                    '33139',
                    'Miami',
                    'US'
                  )
                )
    def xmlCustomer = xstream.toXML(john)
    println xmlCustomer
  4. The generated XML looks as follows:
    <Customer>
      <id>100</id>
      <name>John</name>
      <lastName>Red</lastName>
      <businessAddress>
        <street>Ocean Drive 101</street>
        <postcode>33139</postcode>
        <city>Miami</city>
        <country>US</country>
      </businessAddress>
    </Customer>

    Very cool indeed.

  5. To convert the XML back into a bean, it's straightforward as well:
    def xstream = new XStream(new DomDriver())
    Customer johnred = xstream.fromXML(xmlCustomer)
    println johnred.id
    println johnred.businessAddress.postcode
  6. The final lines will print:
    100
    33139
    

How it works...

Whenever XStream encounters an object that needs to be converted to/from XML, it delegates to a suitable converter implementation associated with the class of that object.

XStream comes bundled with many converters for common types, including primitives, string, collections, arrays, null, date, etc. XStream also has a default converter that is used when no other converters match a type. This uses reflection to automatically generate the XML for all the fields in an object.

If an object is composed of other objects, the converter may delegate to other converters.

There's more...

With XStream, it is also possible to drive the way the XML gets generated through aliases. There are different types of aliasing that can be applied to the serialization engine.

  • Class aliasing: The name of the root element of the serialized class can be changed by applying an alias at the class level:
    xstream.alias('Person', Customer)

    The serialization process generates the following XML:

    <Person>
      <id>100</id>
      <name>John</name>
      ...
    </Person>
  • Field aliasing: The name of the bean field can also be modified when it gets serialized:
    xstream.aliasField('customer-id', Customer, 'id')

    This changes the element name id into customer-id:

    <Customer>
      <customer-id>100</customer-id>
      ...
    </Customer>
  • Attribute aliasing: In case you need to convert a bean field into an XML attribute, XStream gets you covered. Let's say that we want to make the id field an attribute of the <Person> element. It couldn't be easier:
    xstream.useAttributeFor(Customer, 'id')

    The resulting serialized XML looks like:

    <Person id="100">
      <name>John</name>
    </Person>

See also

The documentation for XStream is extremely good, and if you want to use this tool to its full potential, it is recommended to spend some time reading the manual at http://xstream.codehaus.org/.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.134.133