XStream – XML To Java

In the earlier tutorial we looked at a way to convertJava to XML and back. In this tutorial we look at some more examples of XML To Java Conversion. XStream can convert XML elements to multiple types depending on the type specified in the Java class. For example a series for similarly named elements can be converted to an array of String, a List of String or an enum. XStream uses an appropriate Converter based on type specified in the java class. Lets look at some examples.

In this first example we have an XML that has a parent element called ‘Music’. It has a child element called type. We populate a Music object that has a ‘type’ field which is an enum.

package com.studytrails.xml.xstream;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;

public class ConverterExample3 {
	static String xml1 = "<Music><type>rock</type></Music>";

	public static void main(String[] args) {
		XStream xStream = new XStream();
		xStream.processAnnotations(Music.class);
		Music musicObject = (Music) xStream.fromXML(xml1);
		System.out.println(musicObject);
		// prints Music [type=ROCK]
	}

}

@XStreamAlias("Music")
class Music {
	public enum genre {
		ROCK, JAZZ
	};

	genre type;

	@Override
	public String toString() {
		return "Music [type=" + type + "]";
	}

}

	
	

In the next examples we convert the genre into a List of Strings

package com.studytrails.xml.xstream;

import java.util.List;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;

public class ConverterExample4 {

	static String xml = "<Music><type>rock</type><type>jazz</type></Music>";

	public static void main(String[] args) {
		XStream xStream = new XStream();
		xStream.processAnnotations(Music2.class);
		Music2 musicObject = (Music2) xStream.fromXML(xml);

		System.out.println(musicObject);
	}

}

@XStreamAlias("Music")
class Music2 {

	@XStreamImplicit
	List<String> type;

	@Override
	public String toString() {
		return "Music2 [type=" + type + "]";
	}

}

In this example The Music elements has a sequence of Artist elements each with an album name and a author name. We convert that to Music Object containing a map of Albums.

package com.studytrails.xml.xstream;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;

public class ConverterExample6 {

	static String xml = "<Music><album><name>name1</name><author>author1</author></album></Music>";

	public static void main(String[] args) {
		XStream xStream = new XStream();
		xStream.processAnnotations(Music4.class);
		xStream.alias("name", String.class);
		xStream.alias("author", String.class);
		xStream.alias("album", Entry.class);
		Music4 musicObject = (Music4) xStream.fromXML(xml);

		System.out.println(musicObject);
	}

}

@XStreamAlias("Music")
class Music4 {

	@XStreamImplicit
	Map<String, String> albums = new HashMap<String, String>();

	@Override
	public String toString() {
		return "Music4 [testMap=" + albums + "]";
	}

}

In the last example lets look at how to deserialize a Date Object. We use a DateConverter where we can specify a format.

package com.studytrails.xml.xstream;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.converters.basic.DateConverter;

public class ConverterExample7 {
	static String xml = "<Musician><birthdate>04-20-1976 23:00:00</birthdate></Musician>";

	public static void main(String[] args) {
		XStream xStream = new XStream();
		xStream.processAnnotations(Musician.class);
		xStream.registerConverter(new DateConverter("MM-dd-yyyy HH:mm:ss", 
				new String[] {}, TimeZone.getTimeZone("UTC")));
		Musician musician = (Musician) xStream.fromXML(xml);
		System.out.println(musician);
	}

}

@XStreamAlias("Musician")
class Musician {
	private Date birthdate;

	@Override
	public String toString() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		format.setTimeZone(TimeZone.getTimeZone("UTC"));
		return "Musician [birthdate=" + format.format(birthdate) + "]";
	}

}


Leave a Reply

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