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.


Friday Apr 24, 2009

Reason 442 : Failed to enable Virtual Adapter

This Summer I had to go to my native place for some reason and I went home with my office laptop so that I can work remotely.After I reached home and tried to check my emails and some internal Sun sites, I could not connect to my VPN using Cisco VPN Client .So I immediately reached my PC Support guys and they could not help either.So I tried  googling for the problem,tweaked some settings in Symantec Endpoint Protection before getting to this link.This link has a very good desciption with nice screenshots.If you have also faced problem like me,you go ahead and visit this site.

After a quick restart, now I am able to connect to VPN and browse our local intranet sites.

In Summary(or for some reason the above link is broken) this worked for me,

  1. Start Menu
  2. Network
  3. Click on Network Sharing
  4. Click on Manage Networks in the new window opened
  5. Right Click on Cisco Adapter and click on Diaognise.
  6. Reset the Network adapter
  7. Now it says there is limited connectivity.Ignore it and restart the machine.
  8. Now try connecting to VPN.Thats all.Now you are connected.
  9. If you still unable to connect,Click here and search for the problem ;-).
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