Class DOMStream


public class DOMStream
extends DOMStreamBase

This package provides an acceptable middle ground between SAX and DOM techniques for parsing XML. A major drawback with DOM is that it requires the complete document and is very memory-intensive, yet SAX is very low-level and hard to work with. This package converts SAX events from the SAXParser to which it is attached into events for one or more DOM Element handlers provided. These handlers receive DOM elements and can therefore process DOM nodes even if the entire document is not available. This package processes elements efficiently memory-wise, by hanging on only to incomplete elements. Make sure to call ignoreElement on the document element, though, unless you want the entire document to be held in memory!

import net.sourceforge.streamdom.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.w3c.dom.*;

class test_DOMStream implements ElementHandler
public void handleElement( Element e )
String text = "";
if ( e.getFirstChild() != null )
text = e.getFirstChild().getNodeValue();
System.out.println( "Element: " + e.getNodeName() + ": "
+ text );

public static void main(String[] argv)
if ( argv.length != 1 ){
System.err.println("java test_DOMStream filename");

DOMStream streamer = new DOMStream();
SAXErrorHandler logger = new SAXErrorHandler();
streamer.ignoreElement("codeindex"); // the document element tag
streamer.addElementHandler( new test_DOMStream() ); // to call handleElement()

// set up a SAX parser for testing ...
XMLReader parser = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
parser.setContentHandler( streamer );
parser.setErrorHandler( logger );

// now parse the stream
parser.parse( argv[0] );
catch( SAXException e )
System.err.println( e );
catch( ParserConfigurationException e )
System.err.println( e );
catch( e )
System.err.println( e );

See Also:

Fields inherited from class net.sourceforge.streamdom.DOMStreamBase
Constructor Summary
          The DOM Stream, before it can be attached to a SAX parser requires a DOM document builder so that it can provide the ElementHandlers with something to handle.
Method Summary
protected  void addContent(java.lang.String chars)
 void addElementHandler(ElementHandler e)
          Provide a handler for DOM elements.
protected  void initDocument()
protected  void notifyHandlers()
protected  void processElementEnd()
protected  void processElementStart(java.lang.String qName, org.xml.sax.Attributes atts)
 void removeElementHandler(ElementHandler e)
Methods inherited from class net.sourceforge.streamdom.DOMStreamBase
addIgnoredElementHandler, characters, endDocument, endElement, endPrefixMapping, ignorableWhitespace, ignoreElement, processingInstruction, removeIgnoredElementHandler, setDocumentLocator, skippedEntity, startDocument, startElement, startPrefixMapping

Constructor Detail


public DOMStream()
          throws javax.xml.parsers.ParserConfigurationException
The DOM Stream, before it can be attached to a SAX parser requires a DOM document builder so that it can provide the ElementHandlers with something to handle. If no DOM parser is available, we will throw.
Method Detail


protected void initDocument()
initDocument in class DOMStreamBase


protected void processElementStart(java.lang.String qName,
                                   org.xml.sax.Attributes atts)
processElementStart in class DOMStreamBase


protected void addContent(java.lang.String chars)
addContent in class DOMStreamBase


protected void notifyHandlers()
notifyHandlers in class DOMStreamBase


protected void processElementEnd()
processElementEnd in class DOMStreamBase


public void addElementHandler(ElementHandler e)
Provide a handler for DOM elements. This element handler will be managed by DOMStream and be deleted when the DOMStream is destroyed.


public void removeElementHandler(ElementHandler e)