Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / October 2005

Tip: Looking for answers? Try searching our database.

Using XMLEncoder to persist PlainDocument

Thread view: 
Dave Neuendorf - 28 Oct 2005 21:23 GMT
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
Roedy Green - 28 Oct 2005 22:27 GMT
>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.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.