net.sourceforge.streamdom
Class JDOMStream

net.sourceforge.streamdom.DOMStreamBase
  |
  +--net.sourceforge.streamdom.JDOMStream

public class JDOMStream
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.jdom.*;

class test_JDOMStream implements JDOMElementHandler
{
public void handleElement( Element e )
{
System.out.println( "Element: " + e.getName() + ": "
+ e.getText() );
}

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

try{
JDOMStream streamer = new JDOMStream();
SAXErrorHandler logger = new SAXErrorHandler();
streamer.ignoreElement("codeindex"); // or whatever root element
streamer.addElementHandler( new test_JDOMStream() ); // 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:
JDOMElementHandler

Fields inherited from class net.sourceforge.streamdom.DOMStreamBase
handlers
 
Constructor Summary
JDOMStream()
           
 
Method Summary
protected  void addContent(java.lang.String chars)
           
 void addElementHandler(JDOMElementHandler 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(JDOMElementHandler 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

JDOMStream

public JDOMStream()
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(JDOMElementHandler 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(JDOMElementHandler e)