// 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 java.util.ArrayList;

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 SAXDemo {
   public static void main(String[] args) {
      ArrayList<String> result = new ArrayList<String>();
      try {
         SAXParserFactory spf = SAXParserFactory.newInstance();
         SAXParser sp = spf.newSAXParser();
         XMLReader xr = sp.getXMLReader();
         xr.setContentHandler(new MySaxParser(result));
         xr.parse("C:\\demo.xml");
      } catch (FactoryConfigurationError e) {
         e.printStackTrace();
      } catch (ParserConfigurationException e) {
         e.printStackTrace();
      } catch (SAXException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
      for(String item : result) {
          System.out.println(item);
      }
   }
}

class MySaxParser extends DefaultHandler {
   private StringBuffer element = new StringBuffer();
   private ArrayList<String> result;
   public MySaxParser(ArrayList<String> result) {
      this.result = result;
   }
   public void characters(char buf[], int offset, int len) throws SAXException {
      element.append(new String(buf, offset, len));
      return;
   }
   public void startElement(String namespaceURI, String localName, String rawName, Attributes atts) throws SAXException {
      if (rawName.equals("int")) {
         element = new StringBuffer();
      }
      return;
   }
   public void endElement(String namespaceURI, String localName, String rawName) throws SAXException {
      if (rawName.equals("int")) {
         result.add(element.toString());
      }
      return;
   }
}
