I am new to Android application development.
I am developing an application which needs to call a .NET webservice and parse XML data. When I parse normal XML data it works with DOM Parser. But, when XML data is huge then it throws "Out of memory error".
Is there any other parser available in Android to parse huge XML data..
Please help me or suggest me..
I am really need this as soon as possible.
Use the SAX parser. SAX parsers are designed to handle huge XML files. Instead of loading the XML file into memory in one go, it walks over the document element-by-element and notifies you.
Additionally, if the XML file is really big, you may also want to look at how the file is loaded. Don't open the file and feed the entire contents into the SAX parser in one go. Instead, read it chunk-by-chunk (e.g. 4Kb blocks at a time) and feed that into the SAX parser.
Edit: A SAX parser works very differently from a DOM parser. Basically, it just goes through the document one element at a time. Whenever it finds an open or close tag, it calls one of your functions (as a callback) and tells it what the tag is and what the data is (if any). It starts at the beginning and goes through to the end and never goes back. It's serial. This means two things:
More code. Your callback needs to determine what to do when certain tags are encountered, what tags should be skipped, etcetera. A SAX parser doesn't go back, so if you need to remember anything for later, you need to do that all yourself. So yeah, it will be more work to deal with many APIs containing many different tags.
It can parse partial XML. It doesn't care that you feed if just the first 4 Kb of an XML file. It will not generate an error but simply ask for another chunk data when it's done. Only when it encounters a mismatched closing tag (or you stop feeding it data too soon) will it generate an error.
So yeah, it's more work. But the payoff is much greater speed and no problem parsing huge files that would not fit into memory.
Besides using SAX as suggested by Sander, your can try using XmlPullParser
you can find more info here
vtd-xml is the best XML parser for this use case. It is both memory efficient and super fast... Here is a paper to prove this:
http://sdiwc.us/digitlib/journal_paper.php?paper=00000582.pdf