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.
To try the example programs that accompany this article, you will need the following tools:
- Java 2 Standard Edition (J2SE) SDK 1.4
- Your favorite text editor
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);
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);
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();
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();
XMLEncoder is designed to archive graphs of objects. This means that if an object has properties that reference other objects, those objects also will be archived. Note, though, that XMLEncoder will only traverse properties that are exposed via the JavaBean naming conventions (such as setX or 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.
- Example Programs — examples.zip
- Using XMLEncoder —
- Long-Term Persistence for JavaBeans —
- Long-Term Persistence of JavaBeans Components: XML Schema —
- XML Archive DTD —
About the Author
Thornton Rose is a contract software developer in Atlanta, Ga. He can be reached via e-mail at [email protected].
Copyright© 2002, Thornton Rose.