3

I'm trying to parse a large file in android.The xml file size exceeds 2Mb. I'm using this code:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder db = factory.newDocumentBuilder();
InputSource inStream = new InputSource();
inStream.setCharacterStream(new StringReader(XMLResponse));
Document doc = db.parse(inStream);

I got an outofmemory error and I dont know how to fix this problem! I'm working on a Xperia arc device!

2 Answers 2

1

Parsing an XML file using a DOM tree requires a lot of memory because the parser needs to retain a lot of information about your file. I did a few experiments with DOM trees a while ago, and found that parsing a file of size n requires 7n memory space.

You can try to reduce the memory footprint of your program by removing the DOM parser and using a SAX parser instead. SAX psrsers are fundamentally different and require a lot less memory when running.

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

Comments

1

If the document is big then better use XMLReader if you can, that way you don't need to store the whole document in memory at once. Usually that would save me in the past as you rarely need all the xml. More frequently you just need to fetch some content strings and attributes from the document and that saves a lot of memory. You just run through the doc and throw it out while retaining all the useful stuff.

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.