SAX

Estudies4you
difference between SAX and DOM
SAX

SAX (Simple API for XML)
  • Is an event-based sequential access parser API developed by the XML-DEV mailing list for XML documents
  • Provides a mechanism for reading data from an XML document that is an alternative to that provided by the DOM
  • Parsers operate on each piece of the XML document sequentially whereas the DOM operates on the document as a whole
SAX vs DOM
Both SAX and DOM parser have their advantages and disadvantages. Which one is better should depends on the characteristics of your application. SAX parser reads xml document with better speed than DOM.
DOM
SAX
A DOM parser creates a tree structure in memory from the input document and then waits for requests from client.
But a SAX parser does not create any internal structure. Instead, it takes the occurrences of components of a input document as events, and tells the client what it reads as it reads through the input document.
A DOM parser always serves the client application with the entire document no matter how much is actually needed by the client.
But a SAX parser serves the client application always only with pieces of the document at any given time.
With DOM parser, method calls in client application have to be explicit and forms a kind of chain.

But with SAX, some certain methods (usually overriden by the cient) will be invoked automatically (implicitly) in a way which is called "callback" when some certain events occur. These methods do not have to be called explicitly by the client, though we could call them explicitly.

When to Choose SAX
In the following cases, using SAX parser is advantageous than using DOM parser:
  • The input document is too big for available memory (actually in this case SAX is your only choice)
  • You can process the document in small contiguous chunks of input. You do not need the entire document before you can do useful work
  • You bus wan o use e parser o ex rac e in orma ion o in eres , an a your compu a ion will be completely based on the data structures created by yourself
  • Actually in most of applications, we create data structures of our own which are usually not as complicated as the DOM tree
When to Choose DOM
In the following cases, using DOM parser is advantageous than using SAX parser.
  • Your application needs to access widely separately parts of the document at the same time
  • Your application may probably use a internal data structure which is almost as complicated as the document itself
  • Your application has to modify the document repeatedly
  • Your application has to store the document for a significant amount of time through many method calls



To Top