SAX or DOM?
http://developerlife.com/tutorials/?p=28
What is DOM?
DOM gives you access to the information stored in your XML document as a hierarchical object model. DOM creates a tree of nodes (based on the structure and information in your XML document) and you can access your information by interacting with this tree of nodes.
What is SAX?
SAX chooses to give you access to the information in your XML document, not as a tree of nodes, but as a sequence of events! You ask, how is this useful? The answer is that SAX chooses not to create a default Java object model on top of your XML document (like DOM does). This makes SAX faster, and also necessitates the following things:
- creation of your own custom object model
- creation of a class that listens to SAX events and properly creates your object model.
Note that these steps are not necessary with DOM, because DOM already creates an object model for you (which represents your information as a tree of nodes).
When to use DOM
If your XML documents contain document data (e.g., Framemaker documents stored in XML format), then DOM is a completely natural fit for your solution. If you are creating some sort of document information management system, then you will probably have to deal with a lot of document data. An example of this is the Datachannel RIO product, which can index and organize information that comes from all kinds of document sources (like Word and Excel files). In this case, DOM is well suited to allow programs access to information stored in these documents.
However, if you are dealing mostly with structured data (the equivalent of serialized Java objects in XML) DOM is not the best choice. That is when SAX might be a better fit.
When to use SAX
If the information stored in your XML documents is machine readable (and generated) data then SAX is the right API for giving your programs access to this information. Machine readable and generated data include things like:
- Java object properties stored in XML format
- queries that are formulated using some kind of text based query language (SQL, XQL, OQL)
- result sets that are generated based on queries (this might include data in relational database tables encoded into XML).
No comments:
Post a Comment