wood burning stoves*
The moose likes XML and Related Technologies and the fly likes Significance of XML encoding/decoding over Serialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Significance of XML encoding/decoding over Serialization" Watch "Significance of XML encoding/decoding over Serialization" New topic
Author

Significance of XML encoding/decoding over Serialization

Harish Tiruvile
Ranch Hand

Joined: Dec 01, 2005
Posts: 99
while reading XML encoding/decoding i noticed one thing....we can store the state of an object...storing the state of an object in XML file is more effective when compared to ordinary file(ex:notepad .txt)...

So i want to confirm whether storing state in XML file will be more effective than serialization ?

If XML is more effective then please tell me in what way(i knows it supports(ofcourse both supports) OOps concept,other than this)......(correct me if i am wrong)


Will xml replace serialization(because i hope we can extracting the data quickly compared to serialization..so less delay)

if you knows any links that explains this topic(XML vs Serialization)then please attach that link here.

one more thing::in serialization if i serialize an Object then class which come under that Hirarichy will also get serialized automatically(we wont write code for that)...Is it the same case in XML(especially using encoding/decoding technique java.beans.XMLEncoder\XMLDecoder)?
[ July 07, 2006: Message edited by: harish thrivile ]

Giving up is the easiest thing in the world to do..but holding it together when everything seems like falling apart is true strength!!
with regards, Harish.T
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12759
    
    5
Here are some of the things I think about when deciding between Java serialization versus XML.

1. How long will the stored object have to last? The problem being that reading serialized objects after a couple of generations of Java (years) may be tricky but XML should remain readable "forever."
2. Similar to 1 - how many different systems will have to read the serialized object?
3. Is there any chance that the underlying Java class will have to be changed due to changing project requirements? XML is sooooooo much more flexible when handling changes, but a serialized object is stuck in the initial representation.

Bill
Harish Tiruvile
Ranch Hand

Joined: Dec 01, 2005
Posts: 99
Thank you William Brogden for replying ...

From your reply i come to understand when exactly i have to go for XML and serilization....

I have one more doubt:::When i encode a javaBean class say Person then will Age class(one of the instance variable in Person class ) encoded in XML automatically?....i tryed to varify using following code but failed to conclude


In serialization which a serialize an object say "SonOBJ" then entire Objects that come in class hierarchy say "ParentOBJ" ,GrandParentsOBJ gets serialized automatically(we wont write code for that)...

i have tryed whether it works in XML...but i am getting NullPinterException...so couldnot(i am unable to) come to conclusion that XML also does same as Serialization.....

so do we need to encode Age class separately..i mean while encoding Person class if i want to encode instance variable(Here Age class)..do i need to write another encoder logic for that class/Object separately?

i am adding complete code here...if it does encoding automatically them please tell me why i am getting NullPointerException(Dont tell u might be accesing without intialization)



Consider a Class Person and Age









William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12759
    
    5
At first glance this looks like it should work - however:

1. which statement is throwing the NPE?
2. does the XML that is written contain the expected data?
(Being able to inspect the serialized data directly is one advantage of XML serialization.)
Bill
Harish Tiruvile
Ranch Hand

Joined: Dec 01, 2005
Posts: 99
William Brogden ,


1. which statement is throwing the NPE?
2. does the XML that is written contain the expected data?


1. which statement is throwing the NPE?
i am Not getting where exactly i am getting NullPointerException Because,

Class Person is executing properly(checked by giving values for "age" and "name" inside main()method ) .......this implies that class Person and Age is working correctly
So i thought i May be getting NullPointerException from TestXml ....i have noticed one strange thing ..If i am getting Exception from TestXml.java class then in output Error message should precede with Error: (because in System.out.println()method i have written "Error::" ),but Error message is not preceded with Error: text , showing only

is displayed in Command window......

So i think am not getting Exception from Class TestXml.java....

i checked Person and Age class for Exception using try & catch block for every methods in that classes by writing like this..


now also i am getting same kind of error message


I am unable to find from where exactly i am getting NullPointerException.....

2. does the XML that is written contain the expected data?

No it is not Containing Expected data....In XML file i am NOT able to retrive Age Data ..XML file looks like this ...



William Brogden sir,you have not Answered this question
"while encoding Person class if i want to encode instance variable(Here Age class)..do i need to write another encoder logic for that class/Object separately?(suppose if we have to write manually(ourself) another Encode/decode class for Age class....then this might be the reason why i am getting Nullpointer Exception.. )
"
And also,
Where exactly i am getting NullpointerException ?....I am not able to retrive Age data..what may the reason for that?
[ July 09, 2006: Message edited by: harish thrivile ]
Harish Tiruvile
Ranch Hand

Joined: Dec 01, 2005
Posts: 99
Removed NullPointerException,But still not getting Age Data in Xml file

i found out why i was getting NullPointerException..it is because ...in javaBean class Person method getAge() should return Object of class Age...earlier return type was int

Now i have remover Exception...why i am not getting Age data in XML file...in serialization if i store a state of an object then all state((variables)) of an objects refered by that class will get also get stored automatically(ofcourse that class should implement Serializable interface )...can we expect same case in XML?


Previous code::
public int getAge()
{
return ageObj.getAge();
}
*******************Modified version************************
public Age getAge()
{
return ageObj;
}





[ July 10, 2006: Message edited by: harish thrivile ]
Harish Tiruvile
Ranch Hand

Joined: Dec 01, 2005
Posts: 99
Final Observation:

XML that is written does not contain the expected data.
first i wrote a class Person which takes two parameter one int and String types....inside Person class i had created an Age Object class using int data...Person class is having getter and setter methods for Name and Age properties

if i encode the class Person ,in XMl file i am not getting Age data...i got only name data in XMl file



XML that is written contain the expected data.

this time i changed Person class..now it is taking 2 parameter one Age Object and another String..Person class is having getter and setter methods for Name and Age properties,same as that of previous code..This time when i encode Person Object i got the result what i expected...

i made Only 1 change replaced primitive argument int to Age object ...And created Age Object in EncoderLogicImplemented class..as shown below



Can anyone tell me what i can conclude from this....
1)Does Instance variable/DATA written to XMl file depends whether we have declared that perticular instance variable inside Encoder logic class (suppose if we have not written that variable/data inside EncoderLogic class then that variable wont be written in XML file...is it true?)
[ July 10, 2006: Message edited by: harish thrivile ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Significance of XML encoding/decoding over Serialization
 
Similar Threads
Basic Input/Output Question
How can we append/retrieve serialized objects to/from a file?
Why serialization?
Best way to serialize java object
Significance of XML encoding/decoding over Serialization