A comparison of XML Streaming APIs vs. standard J2SE 1.4 SAX Parsing.
Click here here for the original entry.

TestSAXParser class (see also TestSAXParser class)



import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.SAXException;
import org.xml.sax.Attributes;

import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.IOException;

public
class TestSAXParser
{
	private TestContentHandler handler;

	public
	TestSAXParser()
	{
	}

	public
	void
	doParse()
	{
		try {
			handler = new TestContentHandler(this);
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser saxParser_d = factory.newSAXParser();
			InputStream in = new FileInputStream("/temp/index.xml");
			saxParser_d.parse(in, handler);
		}
		catch (FactoryConfigurationError factoryConfigurationError) {
			factoryConfigurationError.printStackTrace();
		}
		catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
		catch (SAXException e) {
			e.printStackTrace();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}

	public
	int
	getStartElementCalls()
	{
		return handler.getStartElementCalls();
	}

	public
	int
	getCharactersCalls()
	{
		return handler.getCharactersCalls();
	}

	public static
	void
	main(String[] args)
	{
    		TestSAXParser parser = new TestSAXParser();
		long start = System.currentTimeMillis();
		parser.doParse();
		long end = System.currentTimeMillis();
		long res = (end-start);
		System.err.println("-----------------------------------------------");
		System.err.println("Start element count = "+parser.getStartElementCalls());
		System.err.println("Characters event count = "+parser.getCharactersCalls());
		System.err.println("-----------------------------------------------");
		System.err.println("Time elapsed = "+res);
		System.err.println("-----------------------------------------------");
	}
}

class TestContentHandler
  extends DefaultHandler
{
  int startElementCalls_d = 0;
  int charactersCalls_d = 0;

	public
	TestContentHandler(TestSAXParser tst)
	{

	}

	public
	void
	endDocument()
		throws SAXException
	{

	}

	public
	void
	startElement(String uri, String localName, String qName, Attributes attributes)
		throws SAXException
	{
    		startElementCalls_d++;
	}

	public
	int
	getStartElementCalls()
	{
		return startElementCalls_d;
	}

	public
	int
	getCharactersCalls()
	{
		return charactersCalls_d;
	}

	public
	void
	characters(char ch[], int start, int length)
		throws SAXException
	{
		charactersCalls_d++;
	}

}