package lecture.sample.xml.sax;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

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

/**
 * @author default
 *
 * Parsing XML document using the Simple API for XML Parsing (SAX)
 */
public class SAXParsingSolution1 extends DefaultHandler{
	
	/**
	 * create a new instance of SAXParsingSample1
	 */
	public SAXParsingSolution1(){
		try {
			// use the JAXP API to instantiate SAXParser in a vendor neutral way
			SAXParser xParser = SAXParserFactory.newInstance().newSAXParser();

			// invoke the instantiated parser with the input XML document
			xParser.parse(new File("text.xml"), this);
		} catch (IOException xEx){
			// error reading the file	
		} catch (SAXException xEx){
			// error parsing the document
		} catch (ParserConfigurationException xEx){
			// error parsing the document
		}
		// when the application reaches this point, parsing is finished
	}




	/**
	 * main application
	 */
	public static void main(String[] args) {
		new SAXParsingSolution1();
	}
	
	
	/**
	 * @see org.xml.sax.ContentHandler#startElement(String, String, String, Attributes)
	 */
	public void startElement(
		String namespaceURI,
		String localName,
		String qName,
		Attributes atts)
		throws SAXException {
		super.startElement(namespaceURI, localName, qName, atts);
		
		// receive an event of a new element is parsed
		if(localName.equals("someTag")){
			// do something	
		} else  {
			// do some other things	
		}
		
		
	}

}