Wednesday Nov 25, 2009

Using Simple Framwork to Serialize Java Objects to XML and Deserialize from XML to Java

Today I came across 2 different Java frameworks: First one is Play and the other is Simple.

I skimmed through the documentation for Play for a few minutes and then I had to go to my lunch and then I completely forgot about that until I decided to write this blog.Will have to give a try this later.Actually while looking at the Play documentation and some other articles in the recent days, I see a clear trend moving  from Centralised repositories like CVSNT ,SVN to Decentralised versioning control systems like Mercurial,GIT,Bazaar(Bazaar says it supports multiple workflows and there is no reason to choose from CVS or DCVS ) etc.

Now lets talk about Simple.Simple is a small Java framework used to serialize Java objects to XML and deserializes XML back to Java(Not sure if there are any other use cases).The home page says "This framework aids the development of XML systems with minimal effort and reduced errors. It offers full object serialization and deserialization, maintaining each reference encountered."

If you look at the javadocs, you will get a clear idea how the various parts of a Java Class are transformed from Java to XML.

For example look at ElementList .This is how object of Type ArrayList are converted.

    <list class="java.util.ArrayList">
       <entry name="one"/>
       <entry name="two"/>
       <entry name="three"/> 
    </list>

Similarly a Map:

    <map class="java.util.HashMap">
       <entry key="one">value one</entry>
       <entry key="two">value two</entry>
       <entry key="three">value three</entry>
    </map>

If you have worked with Spring, this may not be a new thing to you at all.I found the java docs self explanatory and recommend to go through various classes and annotations so that you will get an idea what features we have in this framework.

To start with you need to know about Serializer and Persister classes.Serializer is the interface used to serialize an object to XML and deserialize an object back from XML to Java and Persister is a concrete implementation class for Serializer.

The 2 processes are pretty simple and straight forward.Lets see how can serialize a java object.

1) Get  a Serializer
Serializer serializer = new Persister();

2)Now play with your class that needs to be serialized, like instantiation and then some operations on it or what ever you want.
MyClass myObj = new MyClass("Some data", "any thing you want");

3)Now create a File object to store the serialized XML file.
File targetFile = new File("MyObject.xml");

4)And finally serialize it.
serializer.write(myObj,targetFile);

After this you will see that the object is serialized to an XML file you specified.Similary for deserialization,all you need to do is :

Serializer serializer = new Persister();
File source = new File("MyObject.xml");
MyClass myObj = serializer.read(MyClass.class, source);

Here are some important annotations that you need to know to transform your Java object to XML

   1. @Root (Will be the class)
   2. @Element
   3. @Attribute

If you want to give your own name to the transformed XML  tags ,you can use "name" attribute of the annotations.
Some times you may want to specify some elements as optional.Then you can use "required=false" attribute of Attribute annotation.

 Other annotations that you will need is to convert the Collection objects like ArrayList,Map etc.Here are a few examples:

@ElementArray
private Address[] addresses;

@ElementMap(entry="property", key="key", attribute=true, inline=true)
   private Map<String, String> map;

There are many other important features that you will need if want to use this.If you want to continue learn more about this framework, then visit the nice tutorial page those guys have put up about this framework.Happy reading.


About

This is my personal blog.All the information here reflects my own thoughts and feelings and should not be taken as official information from Oracle.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today