XStream – Referencing Objects

XStream allows you to store references while converting a java object to XML. Consider a case where you have an Artist Object. The object contains albums that the artist has released, but you also want to store a reference to a ‘similar artist’. People listening to an artist might be interested in listening to a similar artist. When you convert this object to XML, XStream preserves the reference to the similar artist. It also manages circular references, so if there are no similar artist then the ‘similar artist’ field could store a reference to the Owner Artist.

XStream has multiple ways to store references. you need to use the setMode(int mode) method to set the mode. XStream allows following modes:

  1. NO_REFERENCES
  2. ID_REFERENCES
  3. XPATH_RELATIVE_REFERENCES
  4. XPATH_ABSOLUTE_REFERENCES
  5. SINGLE_NODE_XPATH_RELATIVE_REFERENCES
  6. SINGLE_NODE_XPATH_ABSOLUTE_REFERENCES

In the example below we see how each of the above looks We first create the XStream object instance, the Artist Class and the Album class. We also add appropriate aliases to make the XML more readable.

package com.studytrails.xml.xstream;

import com.thoughtworks.xstream.XStream;

public class ReferencingObjectsExample {

	public static void main(String[] args) {
		Artist artist = new Artist();
		AnotherAlbum album = new AnotherAlbum();
		artist.setAlbum(album);
		XStream xStream = new XStream();
		xStream.alias("artist", Artist.class);
		xStream.alias("anotherAlbum", AnotherAlbum.class);

Here are the classes. An Artist contains an album. It also contains object of type ‘Top10Album’ which is a child of the class ‘Album’ and contains the top10 songs of the Artist. We also store a reference to another artist which is similar to this artist. The aim is to see how the reference to the various objects are stored in the XML. The reference to Top10Album is a reference to a derived class. The reference to Artist is a reference to the Object’s own type.

class Artist {
	Top10Album album;
	Album album2;
	Artist similarArtist;

	public void setSimilarArtist(Artist similarArtist) {
		this.similarArtist = similarArtist;
	}
	
	public void setAlbum(Top10Album album) {
		this.album = album;
		album2 = (Top10Album) this.album;
	}
}

class Album {
}

class Top10Album extends Album {
}

case 1: ID_REFERENCES

xStream.setMode(XStream.ID_REFERENCES);

The output XML


  
  

case 2: NO_REFERENCES

xStream.setMode(XStream.NO_REFERENCES);

The output XML


  
  

case 3: SINGLE_NODE_XPATH_ABSOLUTE_REFERENCES

xStream.setMode(XStream.SINGLE_NODE_XPATH_ABSOLUTE_REFERENCES);

The output XML


  
  

case 4: SINGLE_NODE_XPATH_RELATIVE_REFERENCES

xStream.setMode(XStream.SINGLE_NODE_XPATH_RELATIVE_REFERENCES);

The output XML


  
  

case 5: XPATH_ABSOLUTE_REFERENCES

xStream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);

The output XML


  
  

case 6: XPATH_RELATIVE_REFERENCES

xStream.setMode(XStream.XPATH_RELATIVE_REFERENCES);

The output XML


  
  

Leave a Comment