File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes Are SAX and DOM Parsers or APIs??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Are SAX and DOM Parsers or APIs???" Watch "Are SAX and DOM Parsers or APIs???" New topic

Are SAX and DOM Parsers or APIs???

Amit Roy
Ranch Hand

Joined: Oct 10, 2000
Posts: 132
I am highly confused as to wether DOM and SAX are parsers or APIs to support XML coz when we talk about parsers we talk about wether it supports SAX or DOM but again when we talk about the two type of parsers we talk about stream based or document which are implemented by Please help
Amit Roy
[This message has been edited by Amit Roy (edited June 02, 2001).]

<I>Chance Favours the Prepared minds"</I>
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

IMO, these are API's. The parsers (JAXP, xerces etc)
implement the API's (mostly interfaces).
I hope someone will correct me if I am wrong.....
- sayta

Take a Minute, Donate an Hour, Change a Life
Mapraputa Is
Leverager of our synergies

Joined: Aug 26, 2000
Posts: 10065
Satya, you are right. Both DOM and SAX are generic APIs for XML. The difference between two:
1) DOM is tree-based and SAX is event-based;
2) DOM is �official� W3C specification, whereas SAX is a product of XML developer community, led by David Megginson. He, by the way, had plans to resign, and it was discussed if SAX API should be passed to W3C. So maybe in the future SAX will obtain an official status too.
�DOM parser� and �SAX parser� are, IMHO, a short way of saying �a parser implementing DOM/SAX API�.

Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
Ajith Kallambella

Joined: Mar 17, 2000
Posts: 5782
To add to what has been said alreay, many of the parsers available today support both DOM and SAX based parsing. Since DOM is a W3 spec, just like any evolving specification, it has undergone quite a lot of changes. DOM Level II and Level III offer more sophisticated document handling and is unquestionably poised to surpass the popularity and flexibility of SAX.
Not to forget, XSLT transformations are DOM based .....

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Amit Roy
Ranch Hand

Joined: Oct 10, 2000
Posts: 132
Is it that we can use dom to write both C++ and java Programs is thr something like DOM API for Java and a DOM API for C++ or is it the same.What i have heard that in the DOM Model we can make objects that correspond to the tree structure of the XML tree and if we want to manipulate any data we can manipulate these objects which in turn manipulates the XML file which in turn manipulates the Database .(this is when we are using XML as a data cache in a system) I have no idea wether what i am saying is right please correct me if i am wrong
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

I suggest you take one step at a time.
There seem to be too many assumptions that you are
making when you say "in turn turn
DOM can be implemented any way you want (Java or C++).
DOM is (as you said) a way to represent data in
a Cache. Esp hierarchial data. Yes, you can manipulate
the data. And to store in a Database, you have to support
other methods which take the manipulated data from your
DOM tree structure and write it to the DB.
ps: DOM does not specify how you generate your data or
how you store it. If only specifies how it is represented
in a tree structure.
Hopefully we can hear more from Ajith/Map....
- satya

Amit Roy
Ranch Hand

Joined: Oct 10, 2000
Posts: 132
Then what is used to put the manupilated data into the database??
Frank Carver

Joined: Jan 07, 1999
Posts: 6920
Let's try and pin this down a bit.
SAX is the "Simple API for XML". Sax is a Java API for stream-based parsing of XML input. Most available Java XML parsers implement one or more versions of the SAX API.
Programs which use SAX are "callback"-based. A program uses the SAX API to register interest in specific tags and/or content, then invokes the parser. The parser runs through the XML stream from start to finish and calls your code whenever information in which you are interested is encountered. It is up to your program to build internal data structures, generate output or whatever else is needed - a SAX parser just lets you know that some data is there.
DOM is the "Document Object Model". DOM is a (largely) language independent model for how an XML doceument can be represented in memory. DOM is defined in terms of a set of operations which may be performed on a document. The precise details of how these operations are performed, and how the document data is actually stored in memory are up to the implementer of the parser and depend to some degree on the language chosen to implement the parser and the API.
DOM was designed by a commitee to be language independent, so it has to provide operations which can be implemented in a variety of languages. This means that using the DOM API can sometimes seem clumsy and unusual in any particular language. A lot of Java DOM parsers are actually implemented using SAX. The DOM parser uses the SAX API to register interest in all elements and content, then as the incoming data stream is parsed by the SAX-parser, the DOM parser catches the tags and content and builds the DOM data structures in memory.
A program which uses the DOM API typically "gulps" a whole XML file or stream into a DOM "Document" object, then traverses it looking for particular elements, attributes or content and performing operations on them or with them.
JDOM is a "Java Document Object Model". Designed to make using XML documents simpler for Java programmers, it performs a similar job to DOM, but in a java-specific way. If you want to write your own Java programs to manipulate XML documents, using JDOM is often a lot easier than using the language-neutral DOM API.
There are lots of parser implementations available, including Xerces, xml4j and so on. Many have no particular names, and are just built-in to other products such as the Resin web server. Sometimes a developer will not be sure which parser to choose and want to try several before deciding, which is where JAXP comes in.
JAXP is the "Java API for XML Parsing" from Sun. It provides a way to write programs which are independent of any particular parser implementation, and abstracts operations like starting a parser, telling it what to parse, and shutting it down when you have finished into a consistent API. JAXP is not a parser. Sun do provide an implementation of a SAX and DOM parser, which is often downloaded and installed with JAXP, but JAXP is really just a way to avoid rewriting your program every time you want to check out a new parser.
XSLT is a stylesheet specification language which allows you to write an XML definition of a transformation of an XML document into some other format. XSLT tools are often used to apply a particular look-and-feel to a simple XML document to make a complex HTML page for display on a web site, but XSLT is also capable of far more powerful transformations. To perform an XSLT transformation you need an XSLT processor. Some recent browsers have XSLT processors built-in, but the vast majority of XSLT processing is done on servers.
XSLT is its own language, so there is not really an XSLT API in the same way as there is for DOM and SAX. The only operations needed are usually starting the XSLT processor, telling it which stylesheet to use, and processing one or more documents. At present there is no equivalent to JAXP for XSLT processors, so if you want to directly use XSLT in this way you will most likely tie your code to a particular vendor's XSLT processor implementation.
As for databases. XML is essentially just a data interchange format, which happens to have some increasingly standardized tools available for it. It has no more to to do with databases than any other aspect of programming.

Read about me at ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Amit Roy
Ranch Hand

Joined: Oct 10, 2000
Posts: 132
well Frank
Your post cleared many of the myths i had 1 more particular question i have is that when we are takin about XML as a data Cache then what is the tool that is used to extract data from the database and put in into the xml file.
And if some 1 can give a detailed explanation of the entire architecture where in the xml is used as a data cache it would be most welcomed. Is there a way where in i can display a image in my post?
[This message has been edited by Amit Roy (edited June 05, 2001).]
[This message has been edited by Amit Roy (edited June 05, 2001).]
I agree. Here's the link:
subject: Are SAX and DOM Parsers or APIs???
It's not a secret anymore!