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
Here is the complete example. In this example we want to create an XML from an object of type JazzArtist2.
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
<artist>
<name>Benny Goodman</name>
<isalive>No</isalive>
<url>http://www.bennygoodman.com/</url>
<album genre="swing">
<title>In Stockholm</title>
<year>1959</year>
</album>
<album genre="swing">
<title>A Jazz Holiday</title>
<year>1928</year>
</album>
</artist>
(Note: This article’s original links is here )