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!
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.