function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
nitin sharmanitin sharma 

Differences between reading and writing using XMLReader class and Reading and Writing XML Using the DOM

Hi , I have gone through the apex guide .However,I still have one doubt ,I hope to get some answer here.

What is the difference between reading and writing using XMLReader class and Reading and Writing XML Using the DOM.

I feel that I can use any of the methods to accomplish my task of parsing the XML response.Is that correct?I am wrong please guide me with the proper explanation.

Well. Both can be used to parse / read / write your XML files and the debate goes back to classic old days whether to use DOM or XMLReader/writer. From Salesforce point, I assume each has its own advantages/disadvantages. 

to use DOM we need to load the complete document into memory, kind of clunky, and there is a high chance that if you load something big, we might hit a Heap size limitation or for Visualforce pages view state

vs XMLReader, which is basically a stream based reader, keeps the memory flat, but its much harder to use than DOM.

One uses DOM parsing vs other using SAX. Google it, SAX vs DOM, you would get more info.. 

Hope this helps

nitin sharmanitin sharma
Thanks karthik

I got your point and I personally like using DOM model  but as u said it has its disadvanatges such as loading in memory ,which can  be a issue with the memory.
However,we can always look at XML file contents and size of the file and if the  contents  are not really big then I guess we we will not have issue with it.

If I have 5 to 6 XMl files and I am going to parse them using DOM model.All the files loded in the memory will be allocated seperate heap size or they will all be combined and then allocated heap size.

Any info on it.

I want to figure out the best way to accomplish such tasks rather than focussing on the both the methods.