XStream: Java to XML

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.

package com.studytrails.xml.xstream;

import java.util.ArrayList;
import java.util.List;

import com.thoughtworks.xstream.XStream;

public class CreateXMLFromMusicArtistObject {

    public static void main(String[] args) {
        CreateXMLFromMusicArtistObject marshaller = new CreateXMLFromMusicArtistObject();
        marshaller.createXMlFromObject();
        marshaller.createXMLFromObjectUsingAlias();
        marshaller.createXMLFromObjectUsingAliasAndImplicitCollection();
    }

The JazzArtist and Album classes

class JazzArtist {
    public String name;
    public boolean isAlive;
    public String url;
    public List<Album> albums = new ArrayList<Album>();

    public JazzArtist(String name, boolean isAlive, String url) {
        this.name = name;
        this.isAlive = isAlive;
        this.url = url;
    }

    public void addAlbum(Album album) {
        albums.add(album);
    }

}

class Album {
    public String title;
    public int noOfrecords;
    public int year;

    public Album(String title, int noOfrecords, int year) {
        this.title = title;
        this.noOfrecords = noOfrecords;
        this.year = year;
    }
}

The first method - without alias and Implicit Collection

private void createXMlFromObject() {
 JazzArtist artist = new JazzArtist("Bix Beiderbecke", false, null);
 Album album1 = new Album("Bix Beiderbecke with the Paul Whiteman Orchestra", 5, 1928);
 Album album2 = new Album("Bix Beiderbecke and His Gang", 6, 1927);
 artist.addAlbum(album1);
 artist.addAlbum(album2);
 XStream xStream = new XStream();
 System.out.println(xStream.toXML(artist));
}

The output from the first method

<com.studytrails.xml.xstream.jazzartist>
  <name>Bix Beiderbecke</name>
  <isalive>false</isalive>
  <albums>
    <com.studytrails.xml.xstream.album>
      <title>Bix Beiderbecke with the Paul Whiteman Orchestra</title>
      <noofrecords>5</noofrecords>
      <year>1928</year>
    </com.studytrails.xml.xstream.album>
    <com.studytrails.xml.xstream.album>
      <title>Bix Beiderbecke and His Gang</title>
      <noofrecords>6</noofrecords>
      <year>1927</year>
    </com.studytrails.xml.xstream.album>
  </albums>
</com.studytrails.xml.xstream.jazzartist>

The second method - with aliases

private void createXMLFromObjectUsingAliasAndImplicitCollection() {
 JazzArtist artist2 = new JazzArtist("Benny Goodman", false, null);
 Album album3 = new Album("In Stockholm", 5, 1959);
 Album album4 = new Album("A Jazz Holiday", 3, 1928);
 artist2.addAlbum(album3);
 artist2.addAlbum(album4);
 XStream xStream2 = new XStream();
 xStream2.alias("artist", JazzArtist.class);
 xStream2.alias("album", Album.class);
 xStream2.addImplicitCollection(JazzArtist.class, "albums");
 System.out.println(xStream2.toXML(artist2));
}

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

<artist>
  <name>Benny Goodman</name>
  <isalive>false</isalive>
  <albums>
    <album>
      <title>In Stockholm</title>
      <noofrecords>5</noofrecords>
      <year>1959</year>
    </album>
    <album>
      <title>A Jazz Holiday</title>
      <noofrecords>3</noofrecords>
      <year>1928</year>
    </album>
  </albums>
</artist>

The third method - with alias and implicit collection

private void createXMLFromObjectUsingAlias() {
 JazzArtist artist2 = new JazzArtist("Benny Goodman", false, null);
 Album album3 = new Album("In Stockholm", 5, 1959);
 Album album4 = new Album("A Jazz Holiday", 3, 1928);
 artist2.addAlbum(album3);
 artist2.addAlbum(album4);
 XStream xStream2 = new XStream();
 xStream2.alias("artist", JazzArtist.class);
 xStream2.alias("album", Album.class);
 System.out.println(xStream2.toXML(artist2));
}

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

<artist>
  <name>Benny Goodman</name>
  <isalive>false</isalive>
  <album>
    <title>In Stockholm</title>
    <noofrecords>5</noofrecords>
    <year>1959</year>
  </album>
  <album>
    <title>A Jazz Holiday</title>
    <noofrecords>3</noofrecords>
    <year>1928</year>
  </album>
</artist>

(Note: This article’s original links is here )

 XStream: XML To Java XStream: Java to XML Using Annotation 

Comments

©2015-2021 Alimy All rights reserved.