[XOM-interest] Builder as ContentHandler

Elliotte Harold elharo at metalab.unc.edu
Sat Mar 29 09:51:12 EDT 2008


Michael Kay wrote:
> Is there any way of building a XOM document from a stream of SAX events?
> That is, something that implements ContentHandler, is called to receive the
> SAX events, and returns the document node?


Not really. What you're doing currently in XOMWriter is probably the 
best option.

I've tried very hard to make XOM a self-contained API; that is, one that 
does not expose a lot of dependencies on other APIs. The goal is to be 
able to do complete XML parsing without having to first learn SAX, DOM, 
JAXP, or any other such thing. With the exception of a few references to 
XMLReader in the builder class, I think I've mostly succeeded in the 
core. This also allows me to avoid making a lot of checks I'd have to 
make in XOMHandler if anyone could call its methods with any data at any 
time, and is an important optimization.

I could potentially add one more method to SAXConverter in 1.3. It would 
have a method signature like this:


public Document convert(ContentHandler handler)
              throws XMLException

That sig wouldn't quite work though. You're probably right that we need 
something with a callback; maybe even a separate class. What do you 
think the minimal signature for that would look like? Maybe

public class SAXConverter {

   public SAXConverter(ContentHandler handler) {
     this,handler = handler;
   }

   // Should this throw an exception if there's
   // an unclosed element in the stream?
   public Nodes getResult() {
     ...
   }

}


-- 
Elliotte Rusty Harold  elharo at metalab.unc.edu
Java I/O 2nd Edition Just Published!
http://www.cafeaulait.org/books/javaio2/
http://www.amazon.com/exec/obidos/ISBN=0596527500/ref=nosim/cafeaulaitA/


More information about the XOM-interest mailing list