Dom really consumes a lot of memory. You have imho, the following alternatives.
It is best to use SAX. Using a saxophone, only a very small amount of memory is used, since almost every element moves from input to output at any time, so the amount of memory is extremely low. However, using sax is not so simple, because compared to dom, it is a little contrary to expectations.
Try Stax, I haven’t tried it myself, but it’s a kind of saxophone on steroids, which is easier to implement and use, in contrast to the fact that you just accept saxophone events that you don’t control, you actually “ask the source” to pass you the elements you if you want it to fit in the middle between dom and sax, it has a memory track that looks like a saxophone, but a more friendly paradigm.
Sax, stax, dom are important if you want to properly save, declare, etc. namespaces and other oddities of XML.
However, if you just need a quick and dirty way, which is likely to be also compatible with the namespace, use plain old lines and writers.
Start displaying the declaration and root element of your “large” document on FileWriter. Then download, using dom, if you like, each individual file. Select the elements that you want to insert into the "large" file, convert them to a string and send them to the author. the writer will be flushed to disk without using a huge amount of memory, and dom will load only one document per iteration. If you also do not have very large files on the input side or plan to run it on a mobile phone, you should not have many memory problems. If dom serializes it correctly, it must save namespace declarations, etc., and the code will be just a bunch of lines more than the one you posted.
Simone gianni
source share