| Sun Java System Application Server |
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.
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.
Use the following application-specific variables for this application:
install_dir is the directory where Application Server is installed.
app_dir is the application base directory: install_dir/samples/xml/apps/sax.
Follow this general procedure:
build.xml, is in the app_dir
directory, specifically, install_dir/samples/xml/apps/sax.
To run the sample application, follow these steps:
install_dir/samples/xml/apps/sax
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
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
asant run
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 .
Copyright © 2006 Sun Microsystems, Inc. All rights reserved.