ContentHandler (Example)

class ElementContentHandler implements ContentHandler {
    
  private ArrayList content = new ArrayList();
  
  public void startElement(String namespaceURI, String localName, String qName, Attributes atts) 
    throws SAXException {
    
    StringBuffer buffer = new StringBuffer( "<");
    buffer.append( qName);
    buffer.append( ">");
    
    content.add( buffer);
  }
  
  public void characters(char[] text, int start, int length) throws SAXException {
    StringBuffer buffer = content.get( content.size()-1);

    if ( buffer != null) { 
      buffer.append( text, start, length);
    }
  }
  
  public void endElement(String namespaceURI, String localName, String localName) 
    throws SAXException {
    
    StringBuffer buffer = content.get( content.size()-1);

    if ( buffer != null) { 
      buffer.append( "<");
      buffer.append( qName);
      buffer.append( ">");
      
      System.out.println( buffer);
      
      content.remove( buffer);
    }
  } 

  // do nothing methods  
  public void setDocumentLocator(Locator locator) {}
  public void startDocument() throws SAXException {}
  public void endDocument() throws SAXException {}
  public void startPrefixMapping(String prefix, String uri) throws SAXException {}
  public void endPrefixMapping(String prefix) throws SAXException {}
  public void skippedEntity(String name) throws SAXException {}  
  public void ignorableWhitespace(char[] text, int start, int length) throws SAXException {}
  public void processingInstruction(String target, String data) throws SAXException {}
}