The trouble with DOM
DOM, the Document Object Model, is a standard designed by the World Wide Web Consortium as
a platform- and language-neutral interface that will allow programs and scripts to dynamically access and update the content, structure and style of documents
.
DOM defines a set of Application Programmers Interfaces (API's) that allow programmers to change the content of a document dynamically.
DOM does actually have some good points going for it.
- You can change anything in a document.
- The document will remain valid XML after any change.
- It supports all XML features.
- It is (fairly) consistent across platforms.
However, the bad outweights the good.
- The object model is faulty (e.g. attributes at DOMNode level)
- It is an object model, not an interface model.
- You cannot create a node from a node, you have to add a node created from a document node.
- To add a node from a different document you first have to import it into the new parent node document node.
- To parse a string containing XML and add the result to a node is... difficult.
- No native type support except strings.
- NodeIterators that are not array's, collection or any other native type.
- No standard XPath query interface.
- getElementsByTagName returns all items with tag name, no option to get the first only.
- Most XPath implementations return all without option for the first only - I know, not part of DOM.
Of course there a reasons and explanations enough and the W3C admits many of these mistakes. That does not make me happy with the result though.
There is a whole industry of competing API's ready to take over from DOM like Java's JDom, PHP's SimpleXML. This is logical as I am far from the first to voice most of these opinions. Then why is not everybody using them? Of course there is some inertia due to the fact the DOM is supported everywhere. More important though - in my opinion - is that all these alternatives solve only some of the problems mentioned above.
Having said all that I must applaud PHP's SimpleXML. In five years of research it has the distinction of being the only new method for using XML
that made me think: Yes! This is a step in the right direction!
