Java Reference
In-Depth Information
DOM Processing
DOM works quite differently from SAX. When an XML document is parsed, the whole XML tree
is assembled in memory and returned to your application encapsulated in an object of type
org.w3c.dom.Document , as Figure 22-5 illustrates.
FIGURE 22-5
After you have the Document object available, you can call the Document object's methods to navigate
through the elements in the document tree starting with the root element. With DOM, the entire document
is available for you to process as often and in as many ways as you want. This is a major advantage over
SAX processing. The downside to this is the amount of memory occupied by the document — there is no
choice, you get it all, no matter how big it is. With some documents the amount of memory required may be
prohibitively large.
DOM has one other unique advantage over SAX. It enables you to modify existing documents and create
new ones. If you want to create an XML document programmatically and then transfer it to an external des-
tination, such as a file or another computer, DOM is a better API for this than SAX because SAX has no
direct provision for creating or modifying XML documents. I'll discuss how you can use a DOM parser in
the next chapter.
NOTE StAX also has the capability for creating XML documents.
ACCESSING PARSERS
The javax.xml.parsers package defines four classes supporting the processing of XML documents:
SAXParserFactory : Enables you to create a configurable factory object that you can use to create
a SAXParser object.
 
 
Search WWH ::




Custom Search