tags:

views:

123

answers:

2

What is the fastest way to add an attribute to the root element of a massive XML file? These files are too large to be read into memory and I'd like to avoid as much of an I/O penalty as possible.

+5  A: 

Aren't you going to have to rewrite the entire file anyway if you're inserting text at or near the beginning? A SAX parser should be adaptable to buffer input (via an InputStream) if you don't want it all in memory at once.

You'll still take the hit of rewriting the entire file but not the memory hit of having it in memory at once. Basically you'll be parsing the file, listening to the SAX events and writing out the new file from those events. Your SAX parser will then also listen for the right circumstances to add an attribute.

cletus
Excellent answer :)
bruno conde
A: 

It's virtually impossible not to rewrite the file in Java (I don't even think you could do this if you were tweaking the files at the system level, but maybe...)--So your question becomes what's the most efficient way to rewrite the file?

If possible, I'd avoid all the XML parsers and write my own. This would require that you can easily identify where in the file this new attribute needs to go. If you can come up with the string to match it, you can read in a little at a time, scan for the insertion point and just insert your extra data when you get there, then keep copying the rest over.

Bill K