I'm trying to use XMLEncoder for the first time. My need is to persist a
PlainDocument to a file. Here is the code I'm using to do this:
ContentDocument content = ...; // where ContentDocument extends
PlainDocument and gets initialized with some text
File file = new File( filename );
try
{
FileOutputStream fileOut = new FileOutputStream( file );
XMLEncoder e = new XMLEncoder( new BufferedOutputStream(
fileOut ) );
e.writeObject( content );
e.close();
}
catch ( IOException ex )
{
JOptionPane.showMessageDialog( null, "Unable to save
content:\n" + ex.getMessage() );
}
In my debugger, I've confirmed that the ContentDocument instance called
"content" indeed contains some text.
When I open the file that is produced, here is what is in it:
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.5.0_04" class="java.beans.XMLDecoder">
<object class="com.neusys.hierarchicalpim.ContentDocument"/>
</java>
Clearly the text has not been included in what has been persisted. What
am I doing wrong?
Thanks,
Dave Neuendorf
>I'm trying to use XMLEncoder for the first time. My need is to persist a
>PlainDocument to a file. Here is the code I'm using to do this:
quoting from http://mindprod.com/jgloss/xml.html#XMLSERIALIZATION
There is another form of serialization that produces XML instead of
binary ObjectOutputStreams. It uses the java.beans.XMLEncoder class.
It does not use the Serializable interface, but writes ordinary
Objects that have JavaBean-style getter and setter methods and a
no-arg constructor. It does not persist fields, but rather properties
(in the Delphi sense, not System. setProperty), implemented with
get/set. Basically it looks for all the get methods, and calls them,
and emits a stream of tags named after the properties. To
reconstitute, XMLDecoder instantiates an Object of the class, and
calls the corresponding set methods from the values in the XML stream.
The source and target classes need not have matching code the way they
do with true serialization. Most trouble using this features comes
from thinking it behaves like ordinary serialization. They have almost
nothing in common.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.