Total DOMination
By Michael Floyd
Without question, the DOM is well documented in the W3C specification, in the documentation of various parser tools, and in the growing collection of XML books on the market. To that effect, the DOM is probably the best-documented component of XML (outside of XML itself), largely because the specification is stable and it's been available for roughly two years.
For all of the information about the DOM and its workings, however, there still seems to be little solid information on how you can use various components of XML. This month, I'd like to take a look at the DOM and show how you can perform actual tasks using it. Of course, the DOM encompasses far more than can be covered in a single article. Instead, I'll provide a brief overview of the DOM, then focus on the Core DOM that's the portion of the API applicable to XML. (I've provided a list of sites where you can get a more complete description of the individual API interfaces in "
Online".) In particular, it's important to understand the Node, NodeList, and NamedNodeMap interfaces. It's also useful to be able to predict and compensate for the DOM's pitfalls.
DOM Overview
When an XML parser loads a document, it essentially scans the document, looks for elements, attributes, text, and so on and constructs a hierarchical tree based on those items. When the parser encounters, say, an element in the source document, it creates a new node in the tree containing a representation of that element.