Serializing Java Objects as XML Los Angeles CA

Until the release of Java 2 Standard Edition (J2SE) Version 1.4, Java objects could only be serialized to a proprietary, binary format.

Local Companies

Moyea Software
92295612365
Hot building, ring street
LA, CA
Interneer Inc.
8005586832 x85
6101 W. Centinela Ave.
Culver City, CA
Cornerstone Concepts Inc
818-247-3909
600 W Broadway
Glendale, CA
Greene Computer Corporation
(818) 956-4961
200 S. Louise Street
Glendale, CA
Corticalx Inc Software Solutions & Technology
818-500-0881
425 E Colorado St
Glendale, CA
Alphatier Systems
818-409-8920
517 Griswold St
Glendale, CA
TimeTECH - Customizable Time and Attendance / Workforce Management Solutions
905-677-7009
7420 Airport Rd 203
Mississauga, CA
Hutchinson & Bloodgood, LLP
(818) 637-5000
101 N. Brand Blvd. #1600
Glendale, CA
Telsoft Solutions
818-545-8680
100 N Brand Blvd
Glendale, CA
Abraxas Technologies Inc
818-502-9100
450 N Brand Blvd
Glendale, CA

provided by: 
Originally published at Internet.com


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: * Java 2 Standard Edition (J2SE) SDK 1.4 * Your favorite text editor

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

Resources

* Example Programs - examples.zip * Using XMLEncoder - http://java.sun.com/products/jfc/tsc/articles/persistence4/index.html * Long-Term Persistence for JavaBeans - http://java.sun.com/products/jfc/tsc/articles/persistence/index.html * Long-Term Persistence of JavaBeans Components: XML Schema - http://java.sun.com/products/jfc/tsc/articles/persistence3/index.html * XML Archive DTD - http://java.sun.com/products/jfc/tsc/articles/persistence3/javabeans.dtd

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.

Author: Thornton Rose

Read article at Internet.com site

Featured Local Company

Moyea Software

92295612365
Hot building, ring street
LA, CA

Related Local Events
Automation Technology Expo West (ATX West)
Dates: 2/9/2010 - 2/11/2010
Location: Anaheim Convention Center
Anaheim, CA
View Details

SOLAR POWER - Exhibition and Conference
Dates: 10/12/2010 - 10/14/2010
Location: Los Angeles Convention & Exhibition Center
Los Angeles, CA
View Details

REAL-TIME & EMBEDDED COMPUTING CONFERENCE - LONG BEACH 2009
Dates: 10/1/2009 - 10/1/2009
Location: Marriott Long Beach
Long Beach, CA
View Details

2009 IEEE Petroleum and Chemical Industry Technical Conference (PCIC 2009)
Dates: 9/14/2009 - 9/16/2009
Location:
Anaheim, CA
View Details

Medical Design & Manufacturing - Trade
Dates: 6/9/2009 - 6/11/2009
Location: CANON COMMUNICATIONS LLC
Los Angeles, CA
View Details