XML SAX Sample Application



XML SAX Sample Application


This document describes how to utilize the XML SAX sample application in conjunction with the Application Server.

This sample application document contains the following sections:



Overview

The XML SAX sample application uses the SAXLocalNameCount program to count the number of unique element local names in an XML document, ignoring the namespace name for simplicity. This example also shows one way to turn on DTD validation and how to use a SAX ErrorHandler.

There are several ways to parse a document using SAX and JAXP. The first step is to bootstrap a parser.

There are two ways to bootstrap a parser: one is to use only the SAX API, the other is to use the JAXP utility classes in the javax.xml.parsers package. The JAXP utility classes in the javax.xml.parsers package will be used in this example.

After bootstrapping a parser/XMLReader, there are several ways to begin a parse. In this example, the SAX API will be used.



Compiling and Assembling the Application



This section contains instructions to build and assemble the XML SAX sample application using a Command Line Interface (CLI).


  1. Go to <install_dir>/samples/xml/apps/sax

    Execute the command asant

    The default target core will be executed to compile the application.


  2. Clean the application project area (optional).

    Execute the command asant clean to remove the sample application assemble and build directories.



Running the Sample Application

To run the sample application, do the following:


  1. Go to <install_dir>/samples/xml/apps/sax and execute the following command:

    asant rundtd

    If your build was successful, the sample displays the following information:

    [java] Local Name "Order" occurs 1 times

    [java] Local Name "Quantity" occurs 5 times

    [java] Local Name "Tax" occurs 1 times

    [java] Local Name "Item" occurs 5 times

    [java] Local Name "Customer" occurs 1 times

    [java] Local Name "Receipt" occurs 1 times

    [java] Local Name "Name" occurs 1 times

    [java] Local Name "Subtotal" occurs 1 times

    [java] Local Name "Total" occurs 1 times

    [java] Local Name "Title" occurs 5 times

    [java] Local Name "ID" occurs 5 times

    [java] Local Name "Cardnum" occurs 1 times

    [java] Local Name "UnitPrice" occurs 5 times

    [java] Local Name "Manifest" occurs 1 times


  2. Go to <install_dir>/samples/xml/apps/sax and execute the following command:

    asant runnodtd

    The application should print the following results:

    [java] Local Name "name" occurs 1 times

    [java] Local Name "reservation" occurs 1 times

    [java] Local Name "seat" occurs 1 times

    [java] Local Name "examples" occurs 1 times

    [java] Local Name "book" occurs 1 times

    [java] Local Name "author" occurs 1 times

    [java] Local Name "a" occurs 1 times

    [java] Local Name "title" occurs 1 times

    [java] Local Name "section" occurs 1 times

    [java] Local Name "departure" occurs 1 times

    [java] Local Name "signing" occurs 1 times

Alternatively, you may run both of the previous tests at the same time by doing the following:


  1. Go to <install_dir>/samples/xml/apps/sax and execute the following command:

    asant run


Generating Javadocs

To understand sample application source code better, you can refer to the related javadocs. To generate javadocs, run the command

asant javadocs

or

asant all

After javadocs are generated, you can access them at

<i nstall_dir>/samples/xml/apps/sax/javadocs/index.html



Troubleshooting





Copyright © 2004 Sun Microsystems, Inc. All rights reserved.

Last Updated March 6, 2004