Search code examples
javaxmldommemory

How does file loading in DOM work?


I've been looking at loading XML files with Java and I just can't seem to decipher a certain part of it. I understand that SAX is a streaming mechanism, but when talking about DOM, various sites talk about the model "loading in the full file" or "loading in the all tags", supported by the recommendation to use SAX with large XML files.

To what degree does DOM actually load the full file? The second I access the root node, does it allocate program memory for every single byte of the file? Does it only load tags until the lowest level when it loads text contents?

I'm going to be working with large files, but random access would be useful and editing is a requirement, so I believe DOM is the best choice for me.

Thanks a lot.


Solution

  • It does load the entire file and constructs a tree structure in memory. Thus, every single tag, attribute and any nested tags (no matter how many levels of nesting) will be loaded. It is just that the constructed tree grows bigger the larger the XML file you have.