JSP and SAX : XML « JSP « Java






JSP and SAX

<%@ page import="javax.xml.parsers.SAXParserFactory,
                 javax.xml.parsers.SAXParser,
                 com.java2s.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.java2s;

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



           
       








Related examples in the same category

1.Using the Core XML tags
2.XML transformation
3.Performing XSL Transformations
4.JSP in pure XML generating conforming XHTML
5.XSLT In JSP
6.XSLT in JSP 2
7.JSP Parsing using the DOM
8.JSP Parsing using JDOM
9.JSP Parsing using the DOM and JSTL
10.JSP Displaying a Subset in XML
11.JSP XML and XSLT transform
12.JSP List of data in the XML document
13.Deal With XML In JSP