Sun Java System Application Server 

Samples Index

The XML SAX Sample Application

This sample application demonstrates using the Simple API for XML (SAX) to parse an XML document. The program SAXLocalNameCount counts the number of unique element names in an XML document, ignoring any namespaces for simplicity. This example also shows how to turn on DTD validation and how to use a SAX ErrorHandler.

NOTE: This is not a J2EE application or a browser-based application.

Overview

One way to parse a document using SAX and JAXP is to bootstrap a parser by using only the SAX API, or by using the JAXP utility classes in the javax.xml.parsers package as shown in this sample.

After bootstrapping a parser/XMLReader, there are several ways to begin a parse. This sample uses the SAX API.

Building and Running the Application

Use the following application-specific variables for this application:

Follow this general procedure:

  1. Build the Application. The build file, build.xml, is in the app_dir directory, specifically, install_dir/samples/xml/apps/sax.
  2. This application does not require deployment.
  3. Run the application, as described below.

Running the Application

To run the sample application, follow these steps:

  1. Change your working directory to install_dir/samples/xml/apps/sax
  2. Enter the command asant rundtd. The application displays the following:
    [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
    
  3. Enter this command asant runnodtd. The application displays the following:
    [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
    
  4. Alternatively, run both of the commands at the same time by executing the command asant run

Troubleshooting

If you can't compile the application, make sure you have asant in your PATH environment variable.

If you don't receive the required results, or you receive an error, make sure you have XML data files under /samples/xml/apps/data directory.



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