4.5 Character Events

<..a handler for SAX characracter events..>
 class MyContentHandler extends DefaultHandler {
      int length;
 
   public void characters(char[] ch, int start, int length){
      this.length += length;
      String s = new String(ch, start, length);
      s = s.trim();
      if( !s.equals("") ){
        System.out.print( s + ' ' );
   }  }
 
   public void startElement(String ns, String sname,
                           String qName, Attributes atts) {
      length = 0;
      if( qName.equals("title") ){
        System.out.println( '\n' );
   }  }
 
   public void endElement(String ns, String sname, String qName){
      if( qName.equals("title") ){
        System.out.println();
        for( int i=0; i<length; i++ ){ System.out.print( '=' ); }
        System.out.println() ;
   }  }
 }
-_-_-
<?xml version="1.0" encoding="UTF-8"?>  
<document title="red" canvas="yellow">  
  <section>  
    <title>First section</title>  
    <sentence>Some text.</sentence>  
    <sentence>More text.</sentence>  
  </section>  
  <section>  
    <title>Second section</title>  
    <sentence>Another text.</sentence>  
    <sentence>Yet more text.</sentence>  
  </section>  
</document>  
First section  
=============  
Some text. More text.  
 
Second section  
==============  
Another text. Yet more text.  

[buffering characters]