Serializing Java Objects as XML

Thornton Rose

6/24/2002

Published as "Serializing Java Objects as XML", 6/27/2002, Gamelan.com.

Copyright © 2002, Thornton Rose.


Until the release of Java 2 Standard Edition (J2SE) Version 1.4, Java objects could only be serialized to a proprietary, binary format. Now, Java objects can be serialized as XML documents, a feature of the new long-term persistence API for JavaBeans. This article illustrates how to use this new feature.

Tools

To try the example programs that accompany this article, you will need the following tools:

XML Archives

Java objects that are serialized using the long-term persistence classes are stored as Java XML archives. Bean.xml is an example of a simple, archived JavaBean. JFrame.xml is an example of an archived GUI component. (See "Resources" for links to more information on the Java XML archive format.)

Java XML archives are created with java.beans.XMLEncoder. They are read with java.beans.XMLDecoder.

Writing Serialized Objects

To write (serialize) a Java object to a file with the serialized object binary format, you use the following algorithm:

   // Create output streams.
   FileOutputStream fos = new FileOutputStream("foo.dat");
   ObjectOutputStream oos = new ObjectOutputStream(fos);

   // Write object.
   oos.writeObject(aFoo);

WriteQuark demonstrates the above algorithm. It is a simple program that serializes an instance of Quark to a file.

Writing Archived Objects

Writing (archiving) a Java object to an XML archive is just as easy as writing it to a serialized object binary file. However, instead of using java.io.ObjectOutputStream, you use java.beans.XMLEncoder. Here is the algorithm:

   // Create output stream.
   FileOutputStream fos = new FileOutputStream("foo.xml");

   // Create XML encoder.
   XMLEncoder xenc = new XMLEncoder(fos);

   // Write object.
   xenc.writeObject(aFoo);

WriteQuarkXML demostrates the above algorithm. It is a simple program that archives an instance of Quark to an XML archive.

Reading Serialized Objects

To read (deserialize) a Java object from a serialized object binary file, you use the following algorithm:

   // Create input streams.
   FileInputStream fis = new FileInputStream("foo.bin");
   ObjectInputStream ois = new ObjectInputStream(fis);

   // Read object.
   Foo aFoo = (Foo) ois.readObject();

ReadObj demonstrates the above algorithm. It is a simple program that deserializes an object from a binary file created by WriteQuark.

Reading Archived Objects

Reading (unarchiving) a Java object from an XML archive is just as easy as reading it from a serialized object binary file. However, instead of using java.io.ObjectInputStream, you use java.beans.XMLDecoder. Here is the algorithm:

   // Create input stream.
   FileInputStream fos = new FileInputStream("foo.xml");

   // Create XML decoder.
   XMLDecoder xdec = new XMLDecoder(fis);

   // Read object.
   Foo aFoo = (Foo) xdec.readObject();

ReadXML demonstrates the above algorithm. It is a simple program that unarchives an object from an XML archive created by WriteQuarkXML.

Object Graphs

XMLEncoder is designed to archive graphs of objects. This means that if an object has properties that references other objects, those objects will also be archived. Note, though, that XMLEncoder will only traverse properties that are exposed via the JavaBean naming conventions (e.g. setX, getX) or a BeanInfo class.

WriteAtomXML is a program that demonstrates archiving an object graph. It archives an instance of Atom that references two instances of Quark. ReadXML can be used to read the archive that WriteAtomXML creates.

Resources