Java – maximum size when parsing XML using DOM
I am currently implementing a rest client that will parse XML response messages Later, it will run on Android devices Therefore, memory and processing speed are a big problem However, there is only one XML response at a time, so it is not a problem to process or save multiple XML documents at one time
As far as I know, there are three ways to parse XML using the Android SDK:
> SAX > XmlPullParser > DOM
After reading these different parsing methods, I got that Sax is recommended for large XML files because it won't save the complete tree in memory like dom
However, I ask myself how big are kilobytes, megabytes? Is there a practical size that doesn't matter if you use Sax or DOM?
Thank you, Robert
Solution
There are no standard limits on the size of XML documents or DOM, so it depends entirely on what the host can handle
When you implement it on Android, you should assume that the memory is very limited, and remember that DOM, XML parser, program logic, display logic, JVM and Android itself must be suitable for the available memory!
As a rule of thumb, you can expect the DOM to consume about four times the size of the source XML document Assuming 512MB of available memory, the target only occupies half of DOM, and finally gets 512 / 8 or the actual maximum 64MB XML document
For security reasons, I'll halve it again to a maximum of 32MB Therefore, if you expect such a large document, I will turn to Sax parsing!
Sax is your choice if you want applications to respond at any speed on large documents Once the first element is read, the Sax parser can start returning results, and the DOM parser needs to read the entire document before sending any output to your program