|
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:
Requirements
This sample requires the following products:
Source Files
Download the Java source code files
or click on the links below to view individual files.
This is the driver program for the application. It uses the classes described below to process the order. DefaultHandler class by implementing
the startElement, characters, and endElement methods to:
This class creates an instance of the XML parser and provides methods to parse XML files, inputstreams, or strings. Orders data in XML format. Instructions
Follow these steps to run the sample:
See Also
JAX API for XML Processing (JAXP) Documentation
(http://java.sun.com/xml/jaxp/docs.html) | |||||||||
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.
|
| ||||||||||||