XML vs serialize file

M

madisonne

Hi!

I'm new to the world of XML and saving information to file. Currently
I'm using serialisation class (immplement Serializable) to save class
information to a file. I'm usinf ObjectOutputStream. I just wonder if
I can do the same using XML file? I just want to save some information
like we save in Microsoft word. I don't need something fast to edit
and save. Anybody have advice? example?

Thank you,

Martin
 
B

Bjorn Abelli

"madisonne" wroye...
I'm new to the world of XML and saving information
to file. Currently I'm using serialisation class
(immplement Serializable) to save class information
to a file. I'm usinf ObjectOutputStream. I just wonder
if I can do the same using XML file?

Yes, as you´re already using serializable objects, you can find something
interesting to use in the java.beans package, XMLEncoder and XMLDecoder:

XMLEncoder e =
new XMLEncoder(
new BufferedOutputStream(
new FileOutputStream("myxmlfile.xml")));

// Write the objects as you would to an ObjectOutputStream

e.writeObject( myObject );

e.close();

== And to read them =====================================

XMLDecoder d =
new XMLDecoder(
new BufferedInputStream(
new FileInputStream("myxmlfile.xml")));

// And then iterate as you would with ObjectInputStream

Object obj = d.readObject();

d.close();


// Bjorn A
 
M

madisonne

Thanks a lot Bjorn!

I didn't know that this class exist. I test it and it works fine for
classes that have a public constructor but I'm also dealing with
classes that have private constructor and doesn't work yet. Any clue?

Thank you again!
Martin
 
T

Thomas Fritsch

madisonne wrote:

The XMLEncoder/XMLDecoder are designed to work with Beans only.
Beans are essentially any Java Objects following some simple design
patterns:
(1) have a public no-argument constructor
(2) expose their properties by public pairs of getXxx/setXxx methods.
(3) optionally expose public pairs of addXxxListener/removeXxxListener
methods
By the way: all GUI-components derived from java.awt.Component are Beans
according to the above.

May be you want to read the tutorial at
http://java.sun.com/docs/books/tutorial/javabeans/whatis/beanDefinition.html

Thomas
 
M

madisonne

Hi!

So XML encoder it is use to JAVA Bean only...ok. perfect so my next
question is...What are we using to keeping class information in a
file? (I know it is a beginner question). Currently we are using
serialize object but the problem happen when you change your class
(add attribut, fct), the old file you saved are not compatible with
the new class. So what is the feature in java to save class
information in a file? I check, and I found that some poeple use XML
file with SAX parser. Is it the best way to doing that? Also, if you
want to save Gui information with your document (like size of a
windows for example) what I should use?

Thanks a lot!!

Martin
 
M

Michael Borgwardt

madisonne said:
Hi!

So XML encoder it is use to JAVA Bean only...ok. perfect so my next
question is...What are we using to keeping class information in a
file? (I know it is a beginner question). Currently we are using
serialize object but the problem happen when you change your class
(add attribut, fct), the old file you saved are not compatible with
the new class.

If that's the only problem, then setting an explicit serial version UID
will fix it. See
http://java.sun.com/developer/technicalArticles/Programming/serialization/
under "version control".
 
M

madisonne

Hi!

Thanks for your anwser! But I asking everybody here, what are you
using to save information in a file? Also I would like to save GUI
information such as posisiton for example. Which mecanism, class to
use? Do you have any example? I know XMLEncoder it's one way to go but
what is the most popular one? Best one?

Thank you
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top