Image

Imagestipe wrote in Imagejava_dev

SAX question

I've just tried switching from the Xerces SAX implementation to the one that comes bundled with Java 1.4 (Crimson) and have run into a bit of a problem. Using the code below, I get two very different behaviours - with Xerces, xml is parsed as it comes in (so the program below will print out "Start element: blah" as soon as it's recieved "<blah>"); with Crimson, the parser seems to wait until the stream has closed before throwing events for anything other than "Start Document".

I'm trying to parse the xml as it comes in off of a network stream which could stay open indefinitely, so waiting for it to close before parsing really isn't an option.

Does anyone know if there's a way to get the latter implementation to behave like the former?



If the code below is run with Java 1.4.x and no command line options, it should use the Crimson parser. If you add -classpath path.to/xercesImpl.jar (assuming you have said jar), it should use the Xerces parser.

import javax.xml.parsers.*;
import org.xml.sax.*;

public class SAXTest {
    public static void main( String[] args ) {
	try {
	    SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
	    parser.parse( System.in, new Handler() );
	} catch( Exception e ) {
	    e.printStackTrace();
	}
    }

}

class Handler extends org.xml.sax.helpers.DefaultHandler {
    public void endDocument() {
	System.out.println("End Document");
	System.exit(0);
    }

    public void endElement( String uri, String localName, String qName) {
	System.out.println("End element: " + qName );
    }

    public void error(SAXParseException e) {
	e.printStackTrace();
    }

    public void startDocument() {
	System.out.println("Start Document.");
    }

    public void startElement( String uri, String localName, String qName, Attributes attributes ) {
	System.out.println("Start element: " + qName );
    }
}