Maximum size when parsing XML using the DOM

I am currently implementing a REST client that will parse XML response messages. It is assumed that it will run on an Android device. Thus, memory speed and processing speed are a pretty big problem. However, there will be only one XML response at a time, so processing or storing multiple XML documents at a time is not a problem.

As I understand it, there are three ways to parse XML with the Android SDK:

  • Sax
  • XmlPullParser
  • Dom

Reading these various parsing methods. I got that SAX is recommended for large XML files since it will not contain a complete tree in memory, such as the DOM.

However, I ask myself what is big in terms of kilobytes, megabytes, ...? Is there a practical size to which it doesn't matter if SAX or DOM is used?

Thanks,
Robert

+6
java android xml
source share
3 answers

There are no standard restrictions for XML documents or the DOM size, so it depends entirely on what the host can handle.

As you deploy to Android, you should take a fairly limited amount of memory and remember the DOM, XML parser, your program logic, display logic, JVM and Android, all of which should fit into the available memory !.

Typically, you can expect the DOM to take up about four times the size of the original XML document. So suppose that 512 MB of available memory, intend to take no more than half of this DOM for you, and you get 512/8 or a practical maximum of 64 MB for an XML document.

Just to be safe, I would be half as much as 32 MB max. Therefore, if you expect many documents of this size I will use for SAX analysis !.

If you want the application to respond at any speed on large documents, SAX is the way to go. The SAX analyzer can start returning results, as soon as the first element is read, the DOM parser must read the entire document before any output is sent to your program.

+9
source share

Excerpt from this article :

DOM guerrillas suffer from bloating memory. With smaller XML sets, this is not such a problem, but as XML grows in size, DOM parses become less efficient, which makes them not very scalable in terms of the growth of your XML. Parsers are a happy environment because they allow you to control parsing, thereby eliminating any complex state management, since the state is always known, and they do not suffer from bloating DOM parsers.

This may be the reason SAX is recommended for the DOM: SAX functions as an XML parser. Also, check out the Wikipedia article for SAX here .

EDIT: To specifically determine the size, you will need to look at your implementation. An example of the size of a DOM Document object in the memory of a Java-based XML parser is here . Java, like many languages, defines some memory-based restrictions, such as the JVM heap size , and the Android / XML DOM API web services can also define some internal restrictions at the discretion of programmers (see here ). There is no single answer regarding the maximum size allowed.

+4
source share

My experience allows me to say that using the DOM, the used memory is 2x file size, but of course, this is just an indication. If there is only one field in the XML tree containing all the data, the memory used is similar to the file size!

+1
source share

All Articles