Sun Java Solaris Communities My SDN Account
 
Code sample

JAXP Using SAX

 
Function
The Java API for XML Processing (JAXP) enables applications to parse and transform XML documents using an API that is independent of a particular XML processor implementation. The Simple API for XML (SAX)is available with the Java API for XML Processing (JAXP). Along with the Document Object Model, SAX is one of two common ways to write software that accesses XML data.
 
Using SAX with JAXP allows developers to traverse through XML data sequentially, one element at a time, using a delegation event model. Each time elements of the XML structure are encountered, an event is triggered. Developers write event handlers to define custom processing for events they deem important.
 
Because all the XML data processed does not need to be kept in runtime memory, SAX is very useful for processing very large XML documents or streams. However, if there is a need to randomly access or modify the XML data, DOM should be used.
 
Topics Covered
This sample performs several functions common to software used to process orders represented in XML format. While parsing the orders, it calculates the number of items and the total price of each order.
 
The OrderHandler class handles implements the startElement and endElement events. When the startElement event is encountered for an Order tag, it resets the number of items and total price counters so they can be computed for the next order. The startElement events for the Item and Price tags will increment the appropriate counter.
 
When the endElement event is encountered for an Order tag, the number of items and total price is printed out for that order.
 
This sample uses the standard Java API for XML available with the Java 2 Platform Software Developer Kit (J2SDK), version 1.4. The code in this sample illustrates the following JAXP-SAX concepts:
 
  • Creating a SAX Parser
  • Creating a SAX event handler/li>
  • Parsing an XML document with an assigned SAX event handler
 

Back to top

Requirements
This sample requires the following products:
 
 

Back to top

Source Files
Download the Java source code files or click on the links below to view individual files.
 
SAXSample.java
 

This is the driver program for the application. It uses the classes described below to process the order.

 
OrderHandler.java
 
This sample extends the DefaultHandler class by implementing the startElement, characters, and endElement methods to:
 
    • Calculate price per order
    • Count items per order
    • Count total orders processed
 
CreateParser.java
 

This class creates an instance of the XML parser and provides methods to parse XML files, inputstreams, or strings.

 
Order.xml
 

Orders data in XML format.

 

Back to top

Result Output

 

Back to top

Javadoc

 

Back to top

Instructions
Follow these steps to run the sample:
 
  • Make sure you have the J2SE SDK, version 1.4 installed on your system. This sample will not work with earlier versions of the J2SDK.
  • Download all Java source code files into a directory on your system.
  • From the directory you created, compile all classes with the command:

    javac *.java

  • Run sample by invoking the command:

    java SAXSample

 

Back to top

See Also
JAX API for XML Processing (JAXP) Documentation
(http://java.sun.com/xml/jaxp/docs.html)
 

Back to top

Oracle is reviewing the Sun product roadmap and will provide guidance to customers in accordance with Oracle's standard product communication policies. Any resulting features and timing of release of such features as determined by Oracle's review of roadmaps, are at the sole discretion of Oracle. All product roadmap information, whether communicated by Sun Microsystems or by Oracle, does not represent a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. It is intended for information purposes only, and may not be incorporated into any contract.