October 26, 2016
Hot Topics:

Serializing Java Objects as XML

  • June 27, 2002
  • By Thornton Rose
  • Send Email »
  • More Articles »

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 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.


About the Author

Thornton Rose is a contract software developer in Atlanta, Ga. He can be reached via e-mail at thornton.rose@mindspring.com.

Copyright© 2002, Thornton Rose.

Page 2 of 2

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel