3

I am developing an app for android, I need to create a rather large XML file.

I have tried this with a DOM document however because it resides in memory it throws an 'out of memory' error.

Can someone suggest an alternative solution? This is a XML file create from scratch.

2

3 Answers 3

2

I would write the XML to a Stream or Writer progressively as to traverse the data you want to turn into XML. This way you don't need much more memory than the original data.

Sign up to request clarification or add additional context in comments.

2 Comments

In the in end I used a writer, it was a little on the slow side but did the job. I managed to create a 700mb xml file. Thankyou to all, each answer was helpful!
I would make sure you are using a BufferedWriter. Without this it can be 10x slower.
1

You will always go out of memory for Large Data size. How about dividing your Data and creating small chuncks and then appending all those together.

Or an other option is using SAX parser which consumes less memory.

Comments

1

Streaming XML is the way to go. The most 'vanilla' approach is probably using StAX: http://docs.oracle.com/javaee/5/tutorial/doc/bnbem.html#bnbff

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.