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.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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.