cancel
Showing results for 
Search instead for 
Did you mean: 

In Java Mapping Whether to SAX or DOM ?

former_member181928
Participant
0 Kudos

While implementing Java Mapping which of the following SAX , DOM parsers is the most efficient way to implement it.

I mean considering the voulume of data in the XML format is large which usage will give me most optimal performance .

It would be nice if someone could give a sample most optimal java code using JAXP where input stream is read and after processing written to output stream.

regards

Nilesh Taunk.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Nilesh,

>>While implementing Java Mapping which of the following SAX , DOM parsers is the most efficient way to implement it?

SAX and DOM are two different ways to parse a XML message. Let me tell you how SAX and DOM works.

SAX parser takes the input XML and parses it element wise. Do it is difficult to trace back or to keep the relationship with parent nodes. This is the reason why SAX is faster than DOM. SAX comes in picture, when parsing is required for large XML docs.

Incase of DOM, scans the whole XML doc and creates a corresponding tree structure in the memory. So it shows all the relationship with the corresponding nodes. Because of this DOM is a bit slower than SAX. DOM is the best to use small XML docs and if want to keep the previous XML struture.

Hope this answers your question.

Regards,

Ajay.

bhavesh_kantilal
Active Contributor
0 Kudos

Hi Nilesh,

Just to add to what Ajay has already stated.

DOM parser, will load the entire XML tree into the MAIN MEMORY and this can cause performance issue if the XML structue is big.

SAX parser parses the XML element by element and therefore has a better performance.

DOM parser is easier to implement when compared to SAX and therfore the choice has to be between the PERFORMACE and EASE of DEVELOPMENT.

I would recommend that you go for SAX parser as it will help in the long run.

For help on the methods available in DOM,

http://java.sun.com/j2se/1.4.2/docs/api/org/w3c/dom/Document.html

Regards,

Bhavesh

Answers (4)

Answers (4)

former_member181928
Participant
0 Kudos

I Have A situation where i have to modify my xml like replacing some tags, new tags addition or modifying some data within the tags.

Is it true that SAX is a read only API.In that case SAX cannot be used to modify a XML document..and we are only left with the option of DOM

please suggest.

regards

NIlesh Taunk.

bhavesh_kantilal
Active Contributor
0 Kudos

Hi Nilesh,

<i>In that case SAX cannot be used to modify a XML document..and we are only left with the option of DOM</i>

Both DOM and SAX parsers are used to parse your XML structure. SAX and DOM parsers enable you to parse ( read your XML structure in non technical words ) and how your create the destination strcuture depends on your programming logic.

Thus, irrespective of whether you use SAX or DOM model, you can create your own destination XML structure on whatever logic you want using the corresponding methods of SAX and DOM parsers.

Hope this helps,

Regards,

Bhavesh

bhavesh_kantilal
Active Contributor
0 Kudos

Hi Nilesh,

Drop in your email id , if you want some excellent documents on both DOM and SAX parsers that will help you understand JAVA mapping better.

Regards,

Bhavesh

former_member181928
Participant
0 Kudos

Hi appreciate your prompt reply.

In the previous post once you said

"In that case SAX cannot be used to modify a XML document..and we are only left with the option of DOM"

and then later you state

"Thus, irrespective of whether you use SAX or DOM model, you can create your own destination XML structure on whatever logic you want using the corresponding methods of SAX and DOM parsers.

"

These 2 statements sort of contradict each other.My only query is just like in DOM we have API's to CreateElement , addattribue , add text do we have similar api's in SAX as well..It would be nice of you if you could provide me with a sample code of modifying / creating xml document using SAX..

My email id is - nileshtaunk@rediffmail.com(would be great if you could send me java mapping docs)

regards

Nilesh Taunk.

bhavesh_kantilal
Active Contributor
0 Kudos

Hi Nilesh,

The one in ITALICS in my previous post is the question raised by you and not my answer. Hope this clarifies.

Regards,

Bhavesh

bhavesh_kantilal
Active Contributor
0 Kudos

Hi Nilesh,

<i>//My only query is just like in DOM we have API's to CreateElement , addattribue , add text do we have similar api's in SAX as well</i>

This can be overcome using the programming language and and so to create an Element in SAX, all you will have to do is use a method write provided by your JAVA API..

So, to create a XML structure like,

<welcome>

<XI> Greate </XI>

</welcome>

write("<welcome>");

write("<XI>");

write("great");

write("</XI>");

write("</welcome>");

As I told you earlier, DOM provides EASE of DEVELOPMENT, but in SAX you will have to create the logic for everything including writing your output.

Regards,

Bhavesh

Message was edited by: Bhavesh Kantilal

MichalKrawczyk
Active Contributor
0 Kudos

Hi Nilesh,

>>>While implementing Java Mapping which of the following SAX , DOM parsers is the most efficient way to implement it.

there's no one good answer fot this question

except: it depends:)

SAX and DOM are a little different parsers

so it depends what will your java mapping

have to do: if it will change the

structure or maybe it will only change a few tags,

or maybe it will do something else

have a look at the page below to read a simple

comparision between SAX and DOM

http://www.scit.wlv.ac.uk/~jphb/cp2101/week4/XML_Parsing.html

or this thread:

http://www.biglist.com/lists/xsl-list/archives/200301/msg01318.html

they will give you some idea which one to choose

depending on your requirements

BTW

remember use java mappings only

if you cannot use graphical mapping (very easy debugging)

and if your document cannot be parsed (maybe huge documents)

with XSLT which is supported by many graphilal tools

Regards,

michal

-


<a href="/people/michal.krawczyk2/blog/2005/06/28/xipi-faq-frequently-asked-questions">XI FAQ - Frequently Asked Questions</a>

former_member181928
Participant
0 Kudos

In a Java Mapping Code there are Basically Three Steps

Step 1 - Read InputStream

Step 2 - Process the Data Read From Input Stream

Step 3 - Write the Processed Data To output Strea.

For transferring the data from input to Output stream i am using javax.xml.transform.Transformer;

Is this the most optimal way to do it or there is any other efficient way also avaliable .

PLease suggest.

regards

Nilesh Taunk.

Former Member
0 Kudos

Hi

Check the following thread,

and this weblog

/people/amjad-ali.khoja/blog/2006/02/07/using-dom4j-in-xi--a-more-sophisticated-option-for-xml-processing-than-sap-xml-toolkit

cheers

Sameer