cancel
Showing results for 
Search instead for 
Did you mean: 

SAX and DOM

Former Member
0 Kudos

Dear All,

How do you decide which to use 'SAX or DOM' for java mapping?

Regards,

Ashish

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Ashish,

It depends on the Size of the XML Document that you are going to handle and the Memory constraints.

DOM Constarints:

1. DOM builds an in-memory tree of an entire document. If the document

is very large, this requires a significant amount of memory.

2.The DOM creates objects that represent everything in the original document,

including elements, text, attributes, and whitespace. If you only care about a

small portion of the original document, it's extremely wasteful to create all

those objects that will never be used.

3.A DOM parser has to read the entire document.For very large documents, this could cause a significant delay.

SAX Constraints:

1.SAX events are stateless. When the SAX parser finds text in an XML

document, it sends an event to your code. That event simply gives you the

text that was found; it does not tell you what element contains that text.

you want to know that, you have to write the state management code

yourself.

2. SAX events are not permanent. If your application needs a data structure

that models the XML document, you have to write that code yourself. If you

need to access data from a SAX event, and you didn't store that data in your

code, you have to parse the document again.

Above all these technical Details.

DOM is easier to code and maintain. It is difficult in case of SAX.

Regards,

Sudharshan

Answers (5)

Answers (5)

Former Member
0 Kudos

Hi,

To add to the discussion, If you are sure that a scenario will not have large XMLs' as input then you can go for DOM parser. Since that is easy to code and it might also give some better time performance(mostly DOM is left out for space performance issues). If u implement such a case in SAX, you might need to traverse the XML more number of times that leads to time performance issues.

If the scenario takes large input XMLs' then you can go for SAX as others said because it is not memory intensive.

The crux is(According to me )

"DOM parsing - for scenarios involving small input files"

"SAX parsing - for scenarios involving large input files"

Regards,

P.Venkat

Former Member
0 Kudos

Hi ,

Whether you choose DOM or SAX is going to depend on several factors:

1) Purpose of the application: If you are going to have to make changes to

the data and output it as XML, then in most cases, DOM is the way to go.

It's not that you can't make changes using SAX, but the process is much

more complex, as you'd have to make changes to a copy of the data

rather than to the data itself.

2) Amount of data: For large files, SAX is a better bet.

3) How the data will be used: If only a small amount of the data will

actually be used, you may be better off using SAX to extract it into your

application. On the other hand, if you know that you will need to refer

back to large amounts of information that has already been processed,

SAX is probably not the right choice.

The need for speed: SAX implementations are normally faster than DOM implementations.It's important to remember that SAX and DOM are not mutually exclusive. You can use DOM to create a stream of SAX events, and you can use SAX to create a DOM tree....

Sekhar

Shabarish_Nair
Active Contributor
0 Kudos

SAX has an advantage in terms of performance. But then its your comfort level at SAX and DOM that wil finally prompt you to select whichever you choose to.

Also ref this link SAX vs DOM - http://developerlife.com/saxvsdom/default.htm to get more idea on the same.

Former Member
0 Kudos

very simple...

as far as possible , use sax parsing and avoid DOM as it it performance intensive. With SAX, your XML structure does not remain into memory but with DOM a tree representing your XML is kept into memory till your parser instance is alive.

bhavesh_kantilal
Active Contributor
0 Kudos

Ashish,

DOM loads the entire XML into your memory and co can cause a perfromance issue if the input XML is too large.

SAX is memoryless parser and so does not load the entire XML into the memory, rather reads element by element and provides a very good perfromance and has no memory issus.

Also, the graphical mapping is also internally converted into a java code internally using SAX parser.

Regards

Bhavesh