[jdom-interest] Streaming JDOM

Gregor Zeitlinger gregor at zeitlinger.de
Sun Jul 16 13:46:05 PDT 2006


Hi,

since I like JDOM, I use it whenever I can.
For large documents, however, I have to use SAX, which seems
uncessarily hard to use.
I have seen that StAX is a better SAX, because it is pull rather than
push based.
It is still somewhat awkward, because you have Event objects and you
have to keep track of endElement events.

It occured to me, that this doesn't need to be the case. It is
possible to have a Streaming XML API (i.e. where not the whole tree is
in memory), which is (almost) as easy to use as JDOM:

//reading
reader.Element rootElement = doc.getRootElement();
for (reader.Element child : element.getChildren()) {
  System.out.println("child: " + child.getName());
  for (reader.Element grandChild : child.getChildren()) {
    System.out.println("grand child: " + grandChild.getName());
    //and so on
  }
}

//writing
writer.Element c1 = root.addChild("c1");
c1.setAttribute("a1", "v1");
c1.addText("text");
c1.addChild("c1.1").setAttribute("a2", "v2");
root.addChild("c2");

You don't need to keep track of endElement events, because it is clear
from the way you use the methods.

Since I like the simplicity of this idea, I was wondering if you would
like to add this "Streaming JDOM" in JDOM.

I have implemented a prototype, which uses SAX to read the XML.

What do you think?

-- 
Gregor Zeitlinger
gregor at zeitlinger.de


More information about the jdom-interest mailing list