[jdom-interest] Streaming JDOM

Mike The Mathematician mikeb at mitre.org
Mon Jul 17 02:47:27 PDT 2006


For large files, I would almost always want
a JDOM tree with a tiny subset of a large xml
file. So I need a way to filter
out children, grandchildren, ...,
before JDOM builds the tree.

Your example below seems to iterate
through an already-built JDOM tree
rather than reducing the in-memory
tree before JDOM builds it.

How do you propose to build only the
part we need, that is, how do you
propose to filter everything else out
from becoming part of the memory tree?



Gregor Zeitlinger wrote:
> 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?
> 




More information about the jdom-interest mailing list