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.

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

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.

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

XStream – XML To Java

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to top