XStream – Java to XML Using Annotation

Convert Java to XML using annotation

In the previous tutorials we saw an example of how to create a java object from XML. That tutorial also explained the concept of aliases and implicit collection. In this tutorial we continue with that but use annotation on the java class. The advantage with annotation is that it is faster to code and develop. the disadvantage is that you bind the java class to XStream.

In the example below we convert an object of type JazzArtist to XML. JazzArtist contains fields name, isAlive, a url and a list of albums. This example uses the following annotations

  • @XStreamAlias – Used on the type, field or attribute. To understand ‘aliases’ look at this tutorial
  • @XStreamImplicit – Used on Collections or array. To understand ‘Implicit Collections/Arrays’ look at the this tutorial.
  • @XStreamAsAttribute – Used to mark a field as an attribute.
  • @XStreamConverter – Use a specific converter for this field. We will look at Converters in detail in a later tutorial. In this example we use a Boolean converter for ‘isAlive’ field, we want the XML to have values yes or no instead of true or false.

Here is the complete example. In this example we want to create an XML from an object of type JazzArtist2.

  • We use annotation to first use an alias of ‘artist’ for JazzArtist2. Each Jazz Artist has a list of albums.
  • In the resulting XML we dont want the albums to appear under the element ‘albums’, rather we want each album to be the child of the artist element. We therefore mark ‘albums’ as an implicit collection using the @XStreamImplicit annotation.
  • We don’t want to include the ‘noOfRecords’ in the resulting XML, we therefore use the @XStreamOmitField to omit that field.
  • We want genre to be an attribute of the album instead of a child. we use @XStreamAsAttribute to mark genre as an attribute.
  • In this example we also use an annotation for a Converter. There is a boolean field in the JazzArtist2 class called isAlive. We dont want the XML to have values ‘true’ or ‘false’ but values ‘yes’ or ‘no’ since they are more human friendly. The @XStreamConverter can be used to specify a Converter and pass values to its constructor.
package com.studytrails.xml.xstream;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.annotations.XStreamConverter;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import com.thoughtworks.xstream.annotations.XStreamOmitField;
import com.thoughtworks.xstream.converters.basic.BooleanConverter;

public class CreateXMLFromMusicArtistObjectAnnotated {

	public static void main(String[] args) throws MalformedURLException {
		CreateXMLFromMusicArtistObjectAnnotated marshaller = 
				new CreateXMLFromMusicArtistObjectAnnotated();
		marshaller.createXMlFromObject();
	}

	private void createXMlFromObject() throws MalformedURLException {
		JazzArtist2 artist2 = new JazzArtist2("Benny Goodman", 
				false, new URL("http://www.bennygoodman.com/"));
		Album2 album3 = new Album2("In Stockholm", 5, 1959,"swing");
		Album2 album4 = new Album2("A Jazz Holiday", 3, 1928,"swing");
		artist2.addAlbum(album3);
		artist2.addAlbum(album4);
		XStream xStream2 = new XStream();
		xStream2.processAnnotations(JazzArtist2.class);
		System.out.println(xStream2.toXML(artist2));
	}
}

@XStreamAlias("artist")
class JazzArtist2 {
	public String name;
	@XStreamConverter(value = BooleanConverter.class,
			booleans = { true }/*is case sensitive*/,
			strings = { "Yes", "No" })
	public boolean isAlive;
	public URL url;
	@XStreamImplicit
	public List<Album2> albums = new ArrayList<Album2>();

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

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

}

@XStreamAlias("album")
class Album2 {
	public String title;
	@XStreamOmitField
	public int noOfRecords;
	public int year;
	@XStreamAsAttribute
	public String genre;

	public Album2(String title, int noOfRecords, int year,String genre) {
		this.title = title;
		this.noOfRecords = noOfRecords;
		this.year = year;
		this.genre = genre;
	}
}


The resulting XML


  Benny Goodman
  No
  http://www.bennygoodman.com/
  
    In Stockholm
    1959
  
  
    A Jazz Holiday
    1928
  
	

Leave a Comment