Java Reference
In-Depth Information
Java supports two complementary APIs for processing an XML document:
SAX , which is the S imple A PI for X ML parsing.
DOM , which is the D ocument O bject M odel for XML.
The support in SDK 1.4 is for DOM level 2 version 1 and for SAX version 2. SDK 1.4 also supports
XSLT version 1.0 where XSL is the e X tensible S tylesheet L anguage and T is T ransformations - a
language for transforming one XML document into another, or into some other textual representation
such as HTML. However, we will concentrate on the basic application of DOM and SAX. XSLT is such
an extensive topic that there are several topics devoted entirely to it.
For more information on XSLT please refer to XSLT Programmer's Reference 2nd Edition , by
Michael H. Kay, Wrox Press Ltd., (ISBN 1-861005-06-7).
Before we get into detail on these APIs, let's look at the broad differences between SAX and DOM, and
get an idea of the circumstances in which you might choose to use one rather than the other.
SAX Processing
SAX uses an event-based process for reading an XML document that is implemented through a callback
mechanism. This is very similar to the way in which we handle GUI events in Java. As the parser reads
a document, each parsing event, such as recognizing the start or end of an element, results in a call to a
particular method that is associated with that event. Such a method is often referred to as a handler . It is
up to you to implement these methods to respond appropriately to the event. Each of your methods
then has the opportunity to react to the event that will result in it being called in any way that you wish.
Below you can see the events that would arise from the XML document example that we saw earlier.
XML
Events in your Program
:
: circle
: radius
: 15
: radius
: position
: x-coordinate
: 30
: x-coordinate
: y-coordinate
: 50
: y-coordinate
: position
: circle
:
Start document
Start element
Start element
Characters
End element
Start element
Start element
Characters
End element
Start element
Characters
End element
End element
End element
End document
<?xml version="1.0">
<circle>
<radius>
15
</radius>
<position>
<x-coordinate>
30
</x-coordinate>
<position>
<y-coordinate>
50
</y-coordinate>
</position>
</circle>
SAX Processing of XML
Each type of event results in a different method in your program being called. There are, for instance,
different events for registering the beginning and end of a document. You can also see that the start and end
of each element result in two further kinds of events, and another type of event occurs for each segment of
document data. Thus this particular document will involve five different methods in your program being
called - some of them more than once, of course, so there is one method for each type of event.
Search WWH ::




Custom Search