// 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 javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

public class TestMySax {
   public static void main(String[] args) throws Exception {
      // create SAX parser
      SAXParserFactory spf = SAXParserFactory.newInstance();
      spf.setValidating(false);
      SAXParser saxParser = spf.newSAXParser();
      XMLReader xmlReader = saxParser.getXMLReader();
      xmlReader.setContentHandler(new MySax());

      // parse file
      xmlReader.parse("file:medarbejdere_noDTD.xml");

      return;
   }
}

class MySax extends DefaultHandler {
   private static StringBuffer content = new StringBuffer();

   public void startDocument() throws SAXException {
      System.out.println("start document");
      return;
   }

   public void endDocument() throws SAXException {
      System.out.println("end document");
      return;
   }

   public void characters (char buf[], int offset, int len) throws SAXException {
      String s = new String(buf, offset, len);
      s = s.trim();
      if(s.length() > 0) System.out.println("characters : " + s);
      return;
   }

   public void startElement(String namespaceURI, String localName, String rawName, Attributes atts) throws SAXException {
      System.out.println("start element : " + rawName);
      for(int i = 0; i < atts.getLength(); i++) {
         System.out.println("attribute: " + atts.getQName(i) + "=\"" + atts.getValue(i) + "\"");
      }
      return;
   }

   public void endElement(String namespaceURI, String localName, String rawName) throws SAXException {
      System.out.println("end element : " + rawName);
      return;
   }
}
