DOM and SAX Tips
DOM and SAX Tips
Disadvantages of the DOM
Although DOM is W3C specification with support for a variety of programming languages, it's not necessarily the best solution for all problems. One of the big issues is that DOM can be memory intensive. As mentioned earlier, when an XML document is loaded, the entire document is read in at once. A large document will require a large amount of memory to represent it. Other parsing methods, such as SAX, don't read in the entire document, so they are better in terms of memory efficiency for some applications.
Some have argued that the DOM API is too complex. Although this is somewhat subjective, it is true that DOM is not practical for small devices, such as PDAs and cellular phones. With the rapid proliferation of these devices and demand for greater functionality, XML will very likely play a role in this market. In these cases, the DOM as specified by W3C might not be the best way to go. Fortunately, there are smaller, simpler APIs for XML manipulation that follow the spirit of the DOM, if not the letter.
Determining available DOM Interfaces
There are a number of extended interfaces that are not mandatory, but might be available in some implementations. You can determine if these interfaces are supported by calling the hasFeature method of the DOMImplementation interface. You can use the arguments "XML" and "2.0" for the feature and version parameters of the hasFeature method.
Advantages of SAX
The DOM is a natural object model of an XML document, but it's not always practical. Large documents can take up a lot of memory. This is overkill if all you want to do is find a small piece of data in a very large document.
SAX is, in many ways, much simpler than the DOM. There is no need to model every possible type of object that can be found in an XML document. This makes the API easy to understand and easier to use. The DOM contains many interfaces, each containing many methods. SAX is comprised of a handful of classes and interfaces. SAX is a much lower-level API when compared with the DOM. For these reasons, SAX parsers tend to be smaller than DOM implementations. In fact, many DOM implementations use SAX parsers under the hood to read in XML documents.
SAX is an event-based API. Instead of loading in an entire document into memory all at once, SAX parsers read documents and notify a client program when elements, text, comments, and other data of interest is found. SAX parsers send you events continuously, telling you what was found next.