[jdom-interest] XMLSerializable, XMLOutputStream, XMLInputStream

Grosjean, Jesse jesse at cs.umd.edu
Tue Jan 2 09:06:47 PST 2001


JDOM is great! 

I use it as a quick way to create file formats for simple java programs that
I make; much like one would use java serialization except XML is easier to
read and not so fragile. My general pattern of use is I add two methods to
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 great
for me, but I'm starting to look for a solution that's more consistent with
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.


Create a set of classes so that we can write and read java objects from XML
in a way similar to java.io.Serilaizable.


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.writeAttribute("x", getX());
  out.writeAttribute("y", getY());
  out.writeAttribute("width", getWidth());
  out.writeAttribute("height", getHeight());

public void readObject(XMLInputStream in) throws IOException {

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

// But if we did this (next line) to the XMLOutputStream before writing the
Rectangle the XML file would look like this

XMLOutputStream out = new XMLOutputStream();
out.mapClassToTag(mypackage.Rectangle, "rect");

<rect x=0 y=0 width=100 height=100>
	<mypackage.UserData>"Some data"<mypackage.UserData/>

//We would then tell the XMLInputStream

in.mapTagToClass("rect", mypackage.Rectangle);

//and everytime the XMLInputStream encountered a <rect> tag it would create
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 love
to hear what people think.

Jesse Grosjean.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: XMLSerializable.zip
Type: application/octet-stream
Size: 2875 bytes
Desc: not available
Url : http://jdom.org/pipermail/jdom-interest/attachments/20010102/c6d5bc6c/XMLSerializable.obj

More information about the jdom-interest mailing list