Simple SAX example in Java

1. SaxExample class

package com.vvirlan;

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

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

public class SaxExample {

	public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
		SaxExample ex = new SaxExample();
		ex.run();
	}

	public void run() throws ParserConfigurationException, SAXException, IOException {

		SAXParserFactory factory = SAXParserFactory.newInstance();
		factory.setNamespaceAware(true);
		SAXParser parser = factory.newSAXParser();
		XMLReader xmlReader = parser.getXMLReader();
		xmlReader.setContentHandler(new MyContentHandler());

		FileInputStream fis = new FileInputStream(new File("C:\\tmp\\myxml.xml"));
		InputSource source = new InputSource(fis);
		xmlReader.parse(source);
	}
}

2. MyHandler

package com.vvirlan;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;

public class MyContentHandler implements ContentHandler {

	@Override
	public void setDocumentLocator(Locator locator) {
		// TODO Auto-generated method stub

	}

	@Override
	public void startDocument() throws SAXException {
		System.out.println("Documemt Started");

	}

	@Override
	public void endDocument() throws SAXException {
		System.out.println("Documemt Ended");

	}

	@Override
	public void startPrefixMapping(String prefix, String uri) throws SAXException {
		// TODO Auto-generated method stub

	}

	@Override
	public void endPrefixMapping(String prefix) throws SAXException {
		// TODO Auto-generated method stub

	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
		System.out.println("element Started "+localName);

	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		System.out.println("element ended "+localName);

	}

	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		// TODO Auto-generated method stub
		System.out.println("Characters: "+new String(ch).substring(start, start+length));

	}

	@Override
	public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
		// TODO Auto-generated method stub

	}

	@Override
	public void processingInstruction(String target, String data) throws SAXException {
		// TODO Auto-generated method stub

	}

	@Override
	public void skippedEntity(String name) throws SAXException {
		// TODO Auto-generated method stub

	}

}

3. The source XML file

<?xml version="1.0" encoding="UTF-8"?>
<records>
	<record>
		<name>Queen I</name>
		<year>1988</year>
		<artist>Queen</artist>
	</record>
		<record>
		<name>Reloaded</name>
		<year>1992</year>
		<artist>Metallica</artist>
	</record>
</records>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s