Using PHP with XML (part 2) - Conclusions... (
Page 5 of 6 )
As you can
see, you can parse a document using either DOM or SAX, and achieve the same result.
The difference is that the DOM parser is a little slower, since it has to build
a complete tree of the XML data, whereas the SAX parser is faster, since it's
calling a function each time it encounters a specific tag type. You should experiment
with both methods to see which one works better for you.
There's another important difference between the two techniques. The SAX approach
is event-centric - as the parser travels through the document, it executes specific
functions depending on what it finds. Additionally, the SAX approach is sequential
- tags are parsed one after the other, in the sequence in which they appear. Both
these features add to the speed of the parser; however, they also limit its flexibility
in quickly accessing any node of the DOM tree.
As opposed to this, the DOM approach builds a complete tree of the document in
memory, making it possible to easily move from one node to another (in a non-sequential
manner.) Since the parser has the additional overhead of maintaining the tree
structure in memory, speed is an issue here; however, navigation between the various
"branches" of the tree is easier. Since the approach is not dependent on events,
developers need to use the exposed methods and attributes of the various DOM objects
to process the XML data.