Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Which is better: Object Output Stream or XMLEncoder???

Status
Not open for further replies.

Oxymoron

Technical User
Dec 17, 2000
168
GB
Hi.

I was going to use XMLEncoder to store a variety of objects in an XML file. However I realise it can only store Java Bean's. Does XMLEncoder not save Object types as class attributes???

Also, in anyone's oppinion, which is better, ObjectOutputStream or XMLEncoder?

Any and all comments, very much appreciated.
Thanks,
Oxy

we are all of us living in the gutter.
But some of us are looking at the stars.
 
When you say "store a variety of objects in an XML file", do you mean you wish to serialize a class into byte[] array data, and save that as an element attribute ?

I've never used XMLEncoder, but from looking at the documentation
it appears as if the class uses introspection - I suspect in fact the java.beans.BeanInfo class - to access the properties of the bean, and store these as xml tags. Hence without getter and setter methods, its no surprise that the XMLEncoder only works with JavaBeans.

Which brings us to Also, in anyone's oppinion, which is better, ObjectOutputStream or XMLEncoder?

They are used for completely differing puposes - so neither is "better".

As I hinted at earlier, ObjectOutputStream is used to serialize (and ObjectInputStream to deserialize) class instances into byte[] data so that they can be written to a physical hard drive, or more commonly exchanged over networks using RMI, EJB, CORBA and other protocols.

Ben
 
PS, I suspect the code that XMLEncoder uses for introspection is something along the lines of
Code:
MyBean tb = new MyBean();
java.beans.BeanInfo bi = java.beans.Introspector.getBeanInfo (tb.getClass());
java.beans.PropertyDescriptor pd[] = bi.getPropertyDescriptors();
for (int i = 0 ; i < pd.length; i++) {
  System.out.print (pd[i].getName() + &quot; &quot;);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top