Java Reference
In-Depth Information
in a given language into its component parts. A natural language processor would have a parser that identi-
fies the grammatical segments in each sentence. A compiler has a parser that identifies variables, constants,
operators, and so on in a program statement. An application accesses the content of a document through an
API provided by an XML parser and the parser does the job of figuring out what the document consists of.
Java supports two complementary APIs for processing an XML document that I'm introducing at a fairly
basic level:
SAX , which is the Simple API for XML parsing
DOM , which is the Document Object Model for XML
The support in JDK 7 is for DOM level 3 and for SAX version 2.0.2. JDK 7 also supports XSLT 1.0 ,
where XSL is the Extensible Stylesheet Language and T is Transformations — a language for transforming
one XML document into another, or into some other textual representation such as HTML. However, I'm
concentrating on the basic application of DOM and SAX in this chapter and the next, with a brief excursion
into using XSLT in a very simple way in the context of DOM in Chapter 23.
Before I 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.
NOTE Javaalsosupportsa streaming API for XML processingcapabilitythatiscalledStAX.
This provides more control of the parsing process than you get with SAX and DOM and re-
quiresalotlessmemorythanDOM.StAXisparticularlyusefulwhenyouareprocessingXML
with limited memory available.
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 you 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 par-
ticular method 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 oppor-
tunity to react to the event, which results in it being called in any way that you want. In Figure 22-4 you can
see the events that would arise from the XML shown.
FIGURE 22-4
 
 
Search WWH ::




Custom Search