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

TestXMLStream class (see also TestSAXParser class)



import javax.xml.stream.*;
import java.io.InputStream;
import java.io.FileInputStream;

public
class TextXMLStream
{
	public static void main(String[] args)
	{
		try {
			InputStream in = new FileInputStream("/temp/index.xml");
			XMLInputFactory factory = XMLInputFactory.newInstance();
			XMLStreamReader parser = factory.createXMLStreamReader(in);
			long start = System.currentTimeMillis();
			int stElementCalls = 0;
			int characterCalls = 0;
			while (true) {
				int event = parser.next();
				if (event == XMLStreamConstants.END_DOCUMENT) {
					parser.close();
					break;
				}
				else if (event == XMLStreamConstants.START_ELEMENT) {
					stElementCalls++;
				}
				else if (event == XMLStreamConstants.CHARACTERS) {
          				characterCalls++;
				}
			}
			long end = System.currentTimeMillis();
			long res = (end-start);
			System.err.println("-----------------------------------------------");
			System.err.println("Start element count = "+stElementCalls);
			System.err.println("Characters event count = "+characterCalls);
			System.err.println("-----------------------------------------------");
			System.err.println("Time elapsed = "+res);
			System.err.println("-----------------------------------------------");
		}
		catch (java.io.IOException e) {
			e.printStackTrace();
		}
		catch (FactoryConfigurationError factoryConfigurationError) {
			factoryConfigurationError.printStackTrace();
		}
		catch (XMLStreamException e) {
			e.printStackTrace();
		}
	}
}