Simple XML-deserialization using SimpleXML

Whenever you need to read-in some parameters to your java-application you have to chose the right format. In order to make the right descision, it is necessary to know what kind of data to store. Things are clear when you have key-value-data like this:
parameter1 = 1
parameter2 = 10
parameter3 = 25

To store such key-value-data you should use java.util.Properties. You can easily load such a file in three lines of code:

//Load
File f = new File(fileName);
Properties properties = new Properties();
properties.load(new FileReader(f));
//Retrieve data
String parameterName = "parameter1";
String property1 = properties.getProperty(parameterName);

However a key-value-store is not appropriate when the data to store

  • has it’s own format or
  • when a key can have more than one property or
  • when you want to map objects to your properties.

If one point is true for your data then you should have a look at XML-format. However, some years ago, it was pain to handle XML in Java (and I don’t speak of object-mapping here…). Gladly, things have changed and there exists the wonderful SimpleXML Framework in the Java-universe that helps you with both XML-serialization (write POJOs to XML) and XML-deserialization (read XML and map them to POJOs).

Read a XML-file

This example refers to the Strategy-Pattern-post I made earlier.

Imagine you want to read-in some calculations via XML. When you have a look at the Strategy-Pattern-Example you will find out, that a calculation consists of:

  • an operator
  • and two variables

Let’s define the class Calculation which stores those properties.

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Root;

@Root(name="calculation")
public class Calculation {

	@Attribute
	private Operator operator;

	@Attribute
	private int valueA;

	@Attribute
	private int valueB;

}

The @Root-annotation says that this class needs can be serialized and mapped to an XML-element. The name-parameter defines the name of this element inside the XML-file. When no name is given, it defaults to the name of the class or field. The @Attribute-annotation specifies fields that are attribues (of an element) in terms of XML-specification.
Because one should have the possibility to calculate more than one calculation, we define the Calculations-class:

import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;

@Root(name="config")
public class Calculations {

	@ElementList(name="calculations")
	private List calculations;

}

This mapping-class is also pretty much straightforward. The @ElementList-annotation allows you to load multiple data-sets from the XML-file. You can also use @ElementArray for arrays of data or @ElementMap for java.util.Map-equivalent. (Note that you have to define the length of the array in your XML-file (length=”123″) as an attribute of the element that holds the annotation when using @ElementArray.)

Now let’s take a look to the XML-file with some example-data:

<config>
	<calculations>
		<calculation operator="ADD" valueA="1" valueB="2"/>
	</calculations>
</config>

We found out that mapping the XML to a POJO was easy. What about loading that file? As you will see, loading files with SimpleXML is also no rocket-science:

Serializer serializer = new Persister();
File file = new File("calculations.xml");
Calculations calculations = serializer.read(Calculations.class, file);

That’s all! No complex file-handling, no new lines of code (except of adding some annotations).
Watch out for my next posts that will show you how to: