XStream – Java to XML

The Problem Statement

In the previous example we saw how to convert a Java Object to XML and back. In this example we look at another example of Java Object to XML Conversion. The example uses an object called a ‘JazzArtist’. This object has a list of ‘Album’ objects besides some other properties. We see here another example of Alias and implicit collection. (To understand alias and implicit collections look at this tutorial) We divide the example in three parts. In the first part we do not use aliases or implicit collections. In the second part we use aliases and in the third part we use an implicit Collection. Note the output from the three parts and observe how aliases and implicit collection change the way the output xml looks.

The JazzArtist and Album classes

The first method – without alias and Implicit Collection

The output from the first method

The second method – with aliases

Output from the second method, the long class names are replaced by the alias.

The third method – with alias and implicit collection

output from the third method, the ‘albums’ element is not required and all the ‘album’ elements are now directly children of the artist.

Leave a Reply

Your email address will not be published. Required fields are marked *