PHP and the Document Object Model (DOM)
Document Object Model (DOM)
Listing 3.1 A Simple XML Document What a wonderful profusion of colors and smells in the market cabbages, tomatoes, apples, aubergines, bananas
Once a DOM parser chewed on this document, it would spit out the tree structure shown in Figure 3.1. Figure 3.1. A DOM tree.
As you can see, the parser returns a tree containing multiple nodes linked to each other by parent-child relationships. Developers can then write code to move around the tree, access node properties, and manipulate node content. This approach is in stark contrast to the event driven approach you studied earlier, "PHP and the Simple API for XML (SAX)." A SAX parser progresses sequentially through a document, firing events based on the tags it encounters and leaving it to the application layer to decide how to process each event. A DOM parser, on the other hand, reads the entire document into memory, and builds a tree representation of its structure; the application layer can then use standard DOM interfaces to find and manipulate individual nodes on this tree, in a non-sequential manner.
PHP and the DOM
PHP 4.0 comes with a primitive, though effective, implementation of the DOM, based on the libxml library. Created by Daniel Veillard, libxml (http://www.xmlsoft.org/) is a
modular, standards-compliant C library that provides XML parsing capabilities to the GNOME project (http://www.gnome.org/). If you're using a stock PHP binary, it's quite likely that you'll need to recompile PHP to add support for this library to your PHP build. (Detailed instructions...