// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.IOException;

import javax.xml.parsers.FactoryConfigurationError;
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.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

public class Parse1Sax {
   private final static String XML_FILE = "C:\\test1.xml";
   public static void main(String[] args) {
      readXml(XML_FILE);
   }
   private static void readXml(String filename) {
      try {
         SAXParserFactory spf = SAXParserFactory.newInstance();
         SAXParser sp = spf.newSAXParser();
         XMLReader xr = sp.getXMLReader();
         xr.setContentHandler(new MySaxParser());
         xr.parse(filename);
      } catch (FactoryConfigurationError e) {
         e.printStackTrace();
      } catch (ParserConfigurationException e) {
         e.printStackTrace();
      } catch (SAXException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

class MySaxParser extends DefaultHandler {
   private final static int CREATE = 1;
   private final static int INSERT = 2;
   private int mode = 0;
   public void startElement(String namespaceURI, String localName, String rawName, Attributes atts) throws SAXException {
      if(rawName.equals("create")) mode = CREATE;
      if(rawName.equals("insert")) mode = INSERT;
      int sqlix = atts.getIndex("SQL");
      if(sqlix >= 0) {
         switch(mode) {
            case CREATE:
               System.out.println("create statement: " + atts.getValue(sqlix));
               break;
            case INSERT:
               System.out.println("insert statement: " + atts.getValue(sqlix));
               break;
            default:
               break;
         }
      }
      return;
  }
}
