progdoc
progdoc

Reputation: 609

DOM vs SAX Java

I have a big xml file that could be downloaded from the internet. To parse it I tried using the DOM parser however it doesn't let me skip certain tags as it gives me an error. Is there a way around this? If i understood correctly the SAX parser allows you to skip tags whilst the DOM doesn't. Can someone kindly clarify this fact, as if that is the case, I can't understand what is the advantage of a DOM parser. Thanks in advance.

Upvotes: 3

Views: 824

Answers (1)

Hip Hip Array
Hip Hip Array

Reputation: 4753

DOM was designed as a language-independent object model to hold any XML data, and as such is a large and complex system. It suits well the two-phase approach of first loading an XML document in, then performing various operations on it. SAX, on the other hand, was designed as a fairly light-weight system using a single-phase approach. With SAX, user-specified operations are performed as the document is loaded. Some applications use SAX to generate a smaller object model, with uninteresting information filtered out, which is then processed similarly to DOM. Note that although DOM and SAX are the well-known "standard" XML APIs, there are plenty of others available, and sometimes a particular application may be better off using a non-standard API. With XML the important bit is always the data; code can be rewritten.

Some quick points:

  1. SAX is faster than DOM.
  2. SAX is good for large documents because it takes comparitively less memory than Dom.
  3. SAX takes less time to read a document where as Dom takes more time.
  4. With SAX we can access data but we can't modify data.With Dom we can modify data.
  5. We can stop the SAX parsing when ever and where ever you want.
  6. SAX is sequential parsing but with DOM we can move to back also.
  7. To parse machine generated code SAX is better.To parse human readable documents DOM is useful.

Upvotes: 12

Related Questions