12

I use both of following , but not working for huge data..of 12 mb

char[] chars = new char[1024];
int len;
while((len=buffer.read(chars))>0) {
    data.append(chars,0,len);
}

and

while ((line = reader.readLine()) != null) {
    sb.append(line + "\n");
}
overbet13
  • 1,654
  • 1
  • 20
  • 36
  • I'm parsing xml .. so whole xml need in single string.. if i stored in sd card then also I have to read it in string which is also require more heap memory... result is crash.. –  Jun 30 '12 at 08:21

8 Answers8

8

Save the data in chunks to the SD card. Then, using Android's built-in XmlPullParser, you can parse the previously saved file the following way:

  1. Open an input stream on the file you just downloaded
  2. Use the method setInput(InputStream inputStream, String inputEncoding)
  3. Have fun while parsing this huge XML file.
overbet13
  • 1,654
  • 1
  • 20
  • 36
3

read in parts 1mb data & use SAXParser for parsing

Vishal Pawar
  • 4,324
  • 4
  • 28
  • 54
2

Why not to read your XML in chunks and save each chunk to SD card? The only problem in this solution is that you need some kind of XML stream parser using which you can dynamically load parts of your XML and parse them.

May be you can use Streaming API for XML, called StaX, and write custom input stream for this case. Some example http://www.vogella.com/articles/JavaXML/article.html#javastax

marwinXXII
  • 1,456
  • 14
  • 21
2
  1. Create XmlPullParser instance
  2. Call XmlPullParser.setInput with your input (either Reader or InputStream); that is the data you download (by Socket or other way)
  3. Use functions of XmlPullParser to parse your xml text file.

Note: you need to do this on non-main thread, it may take long to download the file. (Anyway you can't do network task on UI thread). This should be memory-efficient, XmlPullParser doesn't cache the data in memory. No need to store the file to SD card first.

Also watch for errors: connection time-outs, errors in xml, etc. Treat your job as success when the parser reports that it reached end of document.

Pointer Null
  • 39,597
  • 13
  • 90
  • 111
1

You should probably try allocate the whole 12mb into the memory and then check if the allocation succeeds. Then load the data to the buffer.

Or, if you are able to load the file in parts and process them it should be efficient this way.

codetiger
  • 2,650
  • 20
  • 37
1

Do you need to parse whole XML stream and create one huge DOM object, or will parsing chunks do?

If you MUST parse complete XML than the only thing that you can do is to get more memory and 64bit OS.
If not, use SAX or StAX parser to get and parse the chunks directly from stream or after you save it locally.

Germann Arlington
  • 3,315
  • 2
  • 17
  • 19
1

As stated by must of the user, i would recommend downloading chunk by chunk and not the whole file at once, cause if your are doing it on the main tread it can slowdown all your application, so do it with asynctask and do it chunk by chunk. Apart of that i can recommend you some post about the topic.

-In this post you can find a link to an article about how to use SAX

-In this one you can find another SAX and DOM tutorial

I hope this two articles can help you achieve what you need. Or if both articles fail with your problem, then you may try to find a better way to arrange your XML, there is always a better way of doing it, you just need to think a little bit harder.

UPDATE:

-This other post about STAX may be helpful, cause is a STreaming Api for XML processing, so it may be the best for your case.

Community
  • 1
  • 1
Jorge Aguilar
  • 3,442
  • 30
  • 34
  • Why not instead of trying to reopen your question, you better start searching for another similar questions, or re-do your question so it can be more legible, cause even if it gets open, i do not see a bright future for it. BUT maybe this one can help you [POST ABOUT CARTOONIFYING](http://stackoverflow.com/questions/1357403/how-to-cartoon-ify-an-image-programmatically) – Jorge Aguilar Aug 03 '12 at 13:45
-1

I had same problem and I use this one, You should divide the xml file if possible and then try to read that file into string and parse it.