In this tutorial you will know about sax vs dom parser in java.
Be it java or any language, parsers are the most crucial part of the compilation process on which the efficiency and usability of the language depends to a great extent.
However, when it comes to java language, the parsers: SAX and DOM have forever been the two hit listed parsers, the selection among which most of the times can turn tricky. Here, in this article, an effort is being made to increase the programmer’s knowledge about the two and let him choose the better one according to his needs by a sheer display of differences among them.
SAX Parser
The SAX or Simple API (Application programming interface) for XML is a parser which is used to parse the XML documents using a sequence of occurrences called “events”.
This parser requires a good interaction among the application program and the parser itself since it requires repeated event handling by the parser. Once the parser reads the XML document in a top to bottom fashion, i.e., starting from the beginning of the code and traversing in the left to right fashion towards the bottom; tokens are reported to the application program which in turn send an event handler to be used with the parser. The tokens are identified thereafter and functions in the handler are called.
DOM Parser
The DOM or the Document Object Model can be comparatively easier to understand just because of the familiarity with its structure. It simply converts all the elements of our document into a tree structure. The node of the tree contains data elements. The tree also lets us traverse the structure in an easy manner and perform search and modify operations.
Here is extended information about the differences among the two.
SAX vs DOM Parser
Point of Difference | SAX | DOM |
First published | The SAX parser was introduced particularly after the DOM originated. | October 1, 1998. |
Abbreviated for | Simple API for XML | Document object model. |
Loading | Loads a part of the document in the memory at one time. | Loads the whole XML document in memory at one go. |
A general methodology | Uses the “event handling” method for parsing. Various calls are made to the application program by the parser for acknowledgement of recognition of the tokens created by the parser by reading the document in a top to bottom manner. | Uses the tree methodology. Leads to a tree like structure which contains all the data elements for parsing the document. This structure makes traversing easy. |
Parsing | Parses the document only until we want it to. | Parses the whole document together. |
Functionality | Provides comparatively less functionalities. | Provides sufficient functionalities with the proper data structures. |
Time | The SAX parser is less time consuming. | Consumes more time. |
Space | Occupies less memory space since it does not create any internal structure. | Since it creates an internal structure, it occupies more space in memory. |
Suitable for | Parsing large documents. | Parsing small programs, data lines or configuration lines. |
Nesting | Does not work very well for documents where deep nesting is observed. Also, not suitable for “sorting-like” documents.
|
Can work considerably well for such documents. |
Interface methods | The interface methods are used for accessing the attributes of various elements.
Example: intgetLength()- This function returns the number of attributes. |
The interface methods are used for traversing the tree and performing modifications.
Example: Document.getDocumentElement() – Returns the root element of the document. |
By now, the difference between sax and dom parser must have been very clear. However, It is a sheer matter of the choice of your document which will determine which among the two would be better for you to use. SAX and DOM are undoubtedly the parsers for XML document in Java but they definitely perform their job in a manner distinct to each other which makes choosing them wisely, an important decision for the programmer.