Mega Code Archive

 
Categories / Java / JSP
 

JSP and SAX

<%@ page import="javax.xml.parsers.SAXParserFactory,                  javax.xml.parsers.SAXParser,                  com.rntsoft.MyHandler"%> <html>   <head><title>JSP and SAX</title></head>   <body>     <%       SAXParserFactory factory   = SAXParserFactory.newInstance();       SAXParser        parser    = factory.newSAXParser();       MyHandler        myHandler = new MyHandler(out);       parser.parse("http://localhost:8080/chapter10/people.xml",                    myHandler);     %>   </body> </html> package com.rntsoft; import java.io.IOException; import javax.servlet.jsp.JspWriter; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class MyHandler extends DefaultHandler {   private int       stepCount, totalAge;   private JspWriter out;   private boolean   insideAgeElement;      public MyHandler(JspWriter out)   {     this.out = out;   }   public void startDocument() throws SAXException   {     try      {       out.write(++stepCount + ". Start of document<br>");           }     catch (IOException e)      {       throw new SAXException(e);     }       } // end of startDocument()   public void endDocument() throws SAXException   {     try      {       out.write(++stepCount + ". End of document<p>");       out.write("The total of all ages in the XML document is <b><i>"                 + totalAge + "</i></b>");     }     catch (IOException e)      {       throw new SAXException(e);     }       } // end of endDocument()   public void startElement(String namespaceURI, String localName,                            String qName, Attributes attrs)       throws SAXException   {     if ( qName.equals("age"))      {       insideAgeElement = true;     }          try      {       out.write(++stepCount + ". Start of element: <b>" + qName + "</b>");       int numberOfAttributes = attrs.getLength();       if ( numberOfAttributes > 0 )        {         out.write(". Attributes: <ul>");       } // end of if ()       else          out.write("<br>");              for ( int i=0; i<numberOfAttributes; i++)        {         out.write("<li>" + attrs.getQName(i) + " = "                   + attrs.getValue(i) + "</li>");       } // end of for ()                if ( numberOfAttributes > 0 )        {         out.write("</ul>");       }     }     catch (IOException e)      {       throw new SAXException(e);     }       } // end of startElement()   public void endElement(String namespaceURI, String localName, String qName)       throws SAXException   {     if ( qName.equals("age") )      {       insideAgeElement = false;     }          try      {       out.write(++stepCount + ". End of element <b>" + qName + "</b><br>");           }     catch (IOException e)      {       throw new SAXException(e);     } // end of try-catch        } // end of endElement()   public void characters(char[] chars, int start, int length) throws SAXException   {     String content = new String(chars, start, length);     if ( insideAgeElement )      {       int age  =  Integer.parseInt(content);       totalAge += age;     }          try      {       out.write(++stepCount + ". Character content = ");       if ( length > 0 )          out.write("<b>" + content + "</b><br>");     }     catch (IOException e)      {       throw new SAXException(e);     } // end of try-catch        } // end of characters() } // end of class MyHandler