• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Which parser to choose DOM or SAX?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My requirement is to read the data from XML file in a Java code and then instantiate/set attributes for objects.
Which XML parser should i use DOM or SAX?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If all you are going to be doing is grabbing data from tags in a single pass, SAX will work just fine.
If there is something about the hierarchical structure that is significant, you will probably find DOM easier to work with.
Bill

------------------
author of:
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These days parsers come with both DOM and SAX functionality.
What kind of parsing( DOM/SAX ) you want to use depends on what you want to achieve. For example, if you don't require "peek-ahead" or "look-back" the data in the XML, SAX will do the job well as a sequential parser. If you do need non-sequential access then try DOM.
If you search through this forum you will find some more threads that discuss the tradeoffs between the two parsers.
Good luck,
------------------
Ajith Kallambella M.
Sun Certified Programmer for the Java�2 Platform.
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In addition, DOM does not have its own error handler, so most of time you will have to use a mixture of both.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On this topic, do you know if DOM or JDOM can assist in generating an XML document that conforms specifically to a DTD or schema as it is being generated?
What I thinking is generating a DTD or schema for an XML document to follow. Then I want to use that DTD and generate XML that will conform to that format.
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope.
DOM is a parsing specification. JDOM is a Java based framework that provides a layer over your choice of DOM parser so that you can use standard Java collection classes to manipulate the data elements.
I don't know any such tool that will help you "cook" an XML file from a DTD or a Schema. If one wants to write such a tool, it is not going to be easy - DTD and Schema allows you to define structural details such as recurring elements, order of elements, and a combination of the two. So the mapping between DTD/Schema to an XML document can be one to many ie., there can be many different XML documents that confirm to one DTD/Schema specification. How will the tool choose one correct document over the other? since all of them will be correct documents!!

------------------
Ajith Kallambella M.
Sun Certified Programmer for the Java�2 Platform.
 
sean mcilwain
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I wasn't actually thinking of cooking an XML file, I am wondering if JDOM would actaully throw an exception if you tried to insert an element into the document that didn't conform to the DTD. Or do you need to generate the whole XML document then have a DTD checking parser to determine if the document is valid?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!