Skip to main content

Tip: Stop a SAX parser when you have enough data

Use SAX data without having to parse the entire document

Return to article.


Listing 1. The main application

import org.xml.sax.helpers.XMLReaderFactory;
import org.xml.sax.XMLReader;
import org.xml.sax.SAXException;
import org.xml.sax.InputSource;
import java.io.IOException;

public class MainSaxApp {

  public static void main (String[] args){
    
    try {
       
      String parserClass = "org.apache.crimson.parser.XMLReaderImpl";
      XMLReader reader = XMLReaderFactory.createXMLReader(parserClass);

      WeblogHandler content = new WeblogHandler();
       ErrorProcessor errors = new ErrorProcessor(); 

       reader.setContentHandler(content);
       reader.setErrorHandler(errors);

       InputSource file = new InputSource("changes.xml");
      reader.parse(file);

    } catch (IOException ioe) {
       System.out.println("IO Exception: "+ioe.getMessage());
    } catch (SAXException se) {
       System.out.println(se.getMessage());
    } 
    
  }

}

Return to article.