Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What is DOM SAX

Status
Not open for further replies.

b4x0r

Technical User
May 13, 2004
5
US
I've seen DOM SAX stuff mentioned before.

Pardon me if this isn't the right forum to post on, but could someone explain or direct me to somewhere where I could learn about this stuff?

much thanks,
matt
 
DOM is "Document Object Model". SAX is "Simple API for XML".

I'm learning from a book and from working on a project that uses XML.



Thomas D. Greer
Providing PostScript & PDF
Training, Development & Consulting
 
Here is a simple explanation between the two that I always use by comparing DOM and SAX to women and men. Hate it if you want, but its totally true.

So if we take the stereotype of communication, DOM is like a woman and SAX is like a man.

If an instructor was giving a two minute speech, the woman (DOM) would record the whole speech into memory and be able to tell you everything you want to know about the speech. Luckily, the speech was only two minutes, so it did not take much time to remember it. She can tell you the beginning of the speech, the end, or any part of it. The guy (SAX) can only react to the speech, because it goes in one ear and out the other. If you tell him to remember something after he heard the speech, it ain't gonna happen. The guy (SAX) needs to be aware of what he is listening for and then he can tell you what he hears as he hears it. This can be bad if you want to move back and forth between the speech and review parts that have already been communicated.

Now lets assume the teacher gives a two hour speech. The woman (DOM) is going to have a hell of a time remember everything about the speech. If she attempts to do this, she is going to have memory overload and work really slow to remember the whole speech. Where the guy (SAX) works faster because he only listens to the parts of the speech that he wants to. He doesn't have to remember the whole speech. That's the difference.

DOM pulls the whole XML document into memory. This allows the parser to move forwards, as well as backwards. This allows you to pass the DOM object around and retrieve pieces of that original document. But as the document gets bigger, so does the DOM. And DOM represents every node as its own little mini object, so the more nodes you have, the bigger the document.

SAX reads the document like a stream. It uses event handlers to tell the program that it has met a condition. So if you are looking for a specific element, you can put code in the startElement that says when you read a document that hits the tag "html" (for instance), let me know. SAX can read a document faster because it does not load the document into memory. Unfortunately, SAX cannot move backwards because it reads from start to finish (one ear out the other). The other problem with SAX is that if you hit a piece of content you want, and you don't want to do anymore, you have to read the whole document. The only way to stop SAX is to throw an exception.

This is where StAX comes in. It is a more advanced version of SAX that lets you define when the events get triggered.

Most programming will utilize SAX, DOM is just to memory intensive. You can make it as fast as you want, but you still suck up memory. Just like XSLT translations. They can get faster and faster, but they suck up memory like a hog.

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top