[jdom-interest] Couple of issues I need help on, (memory, sax vs dom, etc)

Laurent Bihanic laurent.bihanic at atosorigin.com
Mon Jul 22 05:07:49 PDT 2002


Hi,

Duffey, Kevin wrote:
> Ok, so below I'll describe each issue and hopefully someone can respond
> to one (or more) of them. I really appreciate any help on these areas.
> 
> 1) XML DTD/Schema validation:

The XML parser needs to read the specified DTD in order to be able to resolve 
entities, regardless validation is requested or not.

The SAX API allows you to resolve the DTD document by yourself: you just need 
to register an org.xml.sax.EntityResolver implementation on the SAX parser 
directly or through SAXBuilder's setEntityResolver. Your implementation can 
then return an InputSource pointing at a local file.

> 3)  Sax vs Dom:

JDOM, just like DOM, aims at providing the programmer with a full memory 
representation of XML documents. Its use of the SAX API is simply a way to get 
the fastest parsing speed.

Now, if you do not need the whole XML document to be loaded in memory, you 
should try using ElementScanner (in package org.jdom.contrib.input.scanner in 
the jdom-contrib CVS module), it does just what you want.
Basically ElementScanner is a SAXFilter on which you register listeners 
associated to XPath-like expressions. When an element matches one of the 
registered XPath expressions, ElementScanner builds the element subtree, 
notifies the associated listener(s) and... forget about this element so that 
the garbage collector can free the memory.

With ElementScanner, your code will look like:
    ElementScanner f = new ElementScanner();
    f.addElementListener(myListener, "header");  // or "/root/header"
    f.parse(new InputSource("test.xml"));
with myListener an instance of a class that implements the ElementListener 
interface. You can register as many XPath expressions as you need, each 
associated to the same or different listeners.

ElementScanner is in sync with the latest JDOM so you'll get some problems if 
you're compiling against beta 8. You'll just have to add two or three throws 
clauses for IOEXceptions.

Hope this helps,

Laurent




More information about the jdom-interest mailing list