[jdom-interest] XMLSerializable, XMLOutputStream, XMLInputStream

Dominik Hasek DHasek1 at excite.com
Wed Jan 3 02:13:35 PST 2001


You idea to serialize java object in xml is great, cause xml means something
at least.

moreover, i don't think it would be useful to jdom, cause jdom is
exclusively designed to construct and manipulate XML valid docs in memory,
and not to serialize any object in xml. I don't think it is the fist jdom
goal. I may be wrong.

I'm not sure but i think sun microsystems has already this idea to make
object serialization in xml. I may be smoking...
i should take a break.

Take care Happy new year...

- DH -

On Tue, 2 Jan 2001 12:06:47 -0500, Grosjean, Jesse wrote:

>  Hi,
>  JDOM is great! 
>  I use it as a quick way to create file formats for simple java programs
>  I make; much like one would use java serialization except XML is easier
>  read and not so fragile. My general pattern of use is I add two methods
>  each of my java objects 
>  writeObject(org.jdom.Element e);
>  	readObject(org.jdom.Element e);
>  And the objects either write themselves into the Element or reconstruct
>  themselves from the Element depending upon the case. This has worked
>  for me, but I'm starting to look for a solution that's more consistent
>  java Serialization and that automates some things like naming elements,
>  adding children, mapping classes to element names and such.
>  I have created a quick hack solution (as a zip attachment), but before I
>  describe that I'll list my questions.
>  1. Is there something else that already does this?
>  2. If not then is it useful to have something that does this?
>  3. If so then would it be a useful extension/add-on to JDOM?
>  4. And finally I would love to hear thoughts and ideas on how I could do
>  this in the best possible way.
>  Here is a quick description of what I have done.
>  Goal: 
>  Create a set of classes so that we can write and read java objects from
>  in a way similar to java.io.Serilaizable.
>  Implementation:
>  This is a quick implementation to sketch the idea, not much time has when
>  into making it clear or complete. Also I'm not an XML expert, I haven't
>  thought about namespace or anything but simple non validated XML
>  // this interface is meant to mirror the Java.io.Serializable interface.
>  public interface XMLSerializable {
>      public void readObject(XMLInputStream in) throws IOException;
>      public void writeObject(XMLOutputStream out) throws IOException;
>  }
>  // this class is meant to take the place of the java ObjectInputStream.
>  public class XMLInputStream {
>    public XMLSerializable readObject();
>    public double readDoubleAttribute(String aName);
>    ...
>    public double readTextContent();
>  }
>  // this class is meant to take the place of the java ObjectOutputStream.
>  public interface XMLOutputStream {
>    public void writeObject(XMLSerializable aSerializable);
>    public void writeAttribute(String aName, String aValue);
>    ...
>  }
>  // Here is an example of how a rectangle would read and write itself.
>  public void writeObject(XMLOutputStream out) throws IOException {
>    out.defaultWriteObject(this);
>    out.writeAttribute("x", getX());
>    out.writeAttribute("y", getY());
>    out.writeAttribute("width", getWidth());
>    out.writeAttribute("height", getHeight());
>    out.writeObject(getUserData());
>  }
>  public void readObject(XMLInputStream in) throws IOException {
>    in.defaultReadObject(this);
>    setRectangularShape(in.readDoubleAttribute("x"),
>    		          in.readDoubleAttribute("y"),
>  		          in.readDoubleAttribute("width"),
>  		          in.readDoubleAttribute("height"));
>   setUserData((UserData)in.readObject());
>  }
>  // In the default case the rectangle would be serialized like this:
>  <mypackage.Rectangle x=0 y=0 width=100 height=100>
>  	<mypackage.UserData>"Some data"<mypackage.UserData/>
>  <mypackage.Rectangle/>
>  // But if we did this (next line) to the XMLOutputStream before writing
>  Rectangle the XML file would look like this
>  XMLOutputStream out = new XMLOutputStream();
>  out.mapClassToTag(mypackage.Rectangle, "rect");
>  out.writeObject(myRectangle);
>  <rect x=0 y=0 width=100 height=100>
>  	<mypackage.UserData>"Some data"<mypackage.UserData/>
>  <rect/>
>  //We would then tell the XMLInputStream
>  in.mapTagToClass("rect", mypackage.Rectangle);
>  //and everytime the XMLInputStream encountered a <rect> tag it would
>  a new mypackage.Rectangle and //tell that object to read itself in.
>  Ok, I hope this that I've gotten across what I'm trying to do. I would
>  to hear what people think.
>  Jesse Grosjean.
>  << File Attachment Removed: "application/octet-stream;
>  	name="XMLSerializable.zip"" >>

Send a cool gift with your E-Card

More information about the jdom-interest mailing list