net.sourceforge.streamdom
Class DOMStream

net.sourceforge.streamdom.DOMStreamBase
  |
  +--net.sourceforge.streamdom.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");
System.exit(-1);
}

try{
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( java.io.IOException e )
{
System.err.println( e );
}
}
}

See Also:
ElementHandler

Fields inherited from class net.sourceforge.streamdom.DOMStreamBase
handlers
 
Constructor Summary
DOMStream()
          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

DOMStream

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

initDocument

protected void initDocument()
Overrides:
initDocument in class DOMStreamBase

processElementStart

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

addContent

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

notifyHandlers

protected void notifyHandlers()
Overrides:
notifyHandlers in class DOMStreamBase

processElementEnd

protected void processElementEnd()
Overrides:
processElementEnd in class DOMStreamBase

addElementHandler

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.

removeElementHandler

public void removeElementHandler(ElementHandler e)