android - Jsoup with big XML -


i'm using jsoup on android application, read xml file webservice restful.

the jsoup library works until xml file contains few number of records.

but when xml 50k or 60k of records, observed jsoup allocate memory until 230mb 240mb. problem because

android:largeheap="true" 

i have 256mb of memory allocable.

this saple code, try yourself

public class mainactivity extends actionbaractivity {  private static handler mhandler = new handler(); private static context context; static textview textview;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      textview = (textview) findviewbyid(r.id.textview);       textview.settext("5 seconds start task");      context = this.getapplicationcontext();      mhandler.postdelayed(new runnable() {         public void run() {             new syncdataws(context).execute();         }     }, 5000);  }  @override public boolean oncreateoptionsmenu(menu menu) {     getmenuinflater().inflate(r.menu.menu_main, menu);     return true; }  @override public boolean onoptionsitemselected(menuitem item) {     int id = item.getitemid();      if (id == r.id.action_settings) {         return true;     }      return super.onoptionsitemselected(item); }  private static class syncdataws extends asynctask<void, string, void> {      context mcontext;      public syncdataws(context context) {         this.mcontext = context;     }      @override     public void onpreexecute() {         super.onpreexecute();         textview.append("\nstart task");     }      @override     protected void doinbackground(void... params) {          try {             publishprogress("start call xml page");             document ws_document = jsoup.connect("xml_example").maxbodysize(0).timeout(10 * 100000).get();             publishprogress("end call xml page");              publishprogress("get rows of document");             elements xml_result_ws = ws_document.select("row");             publishprogress("record number : " + integer.tostring(xml_result_ws.size()));          } catch (ioexception e) {             e.printstacktrace();         }          return null;     }      @override     protected void onprogressupdate(string... values) {         super.onprogressupdate(values);         if (values != null && values.length > 0) {             textview.append("\n" + values[0]);         }     }      @override     protected void onpostexecute(void result) {         textview.append("\nend task");     } } 

}

and example of xml

<xmldataresponse xmlns="www.example.net"><xmldataresult><root xmlns=""><row id="1" id2="2" id3="3" id4="4" f1="0.000000000000000e+000" f2="0.000000000000000e+000" f3="0.000000000000000e+000" f4="" f5="0.000000000000000e+000" f6="0.000000000000000e+000"/></root></xmldataresult></xmldataresponse> 

take row tag , copy + paste until have 60 thousand records in xml example. put wherever want, provided can achievable via http call (url). copy url in code

jsoup.connect("copy url of xml here") 

and can see mean.

i need solution solve allocation issue, because sometimes, not always, allocation arrives @ 256mb , app crashes.

you won't able solve problem jsoup because jsoup creates complete dom tree parsed xml grows bigger , bigger inside memory. jsoup btw. meant html parser in first place.

i'd use event based xml parser xmlpullparser.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -