xml parsing - Android XML Parser only getting last entry -
i'm trying parse xml file list, getting last entry in xml file. have sample below
<?xml version="1.0"?> <stops> <stop> <number>stop_code</number> <lat>stop_lat</lat> <lon>stop_lon</lon> <name>stop_name</name> </stop> <stop> <number>112112</number> <lat> 51.060931</lat> <lon>-114.065158</lon> <name>"crescent heights high school"</name> </stop> <stop> <number>2110</number> <lat> 51.082803</lat> <lon>-114.214888</lon> <name>"eb can olympic rd@olympic ce entr"</name> </stop> ..... <stop> <number>9988</number> <lat> 51.047388</lat> <lon>-114.067770</lon> <name>"nb 2 st@6 av sw"</name> </stop> <stop> <number>9998</number> <lat> 50.997509</lat> <lon>-114.013415</lon> <name>"19 st @ 62 ave se nb ns"</name> </stop>
and pull parser
ublic class pullparser { public static final string stop_name = "name"; public static final string stop_lat = "lat"; public static final string stop_lon = "lon"; public static final string number = "number"; private stops currentstop = null; private string currenttag= null; list<stops> stops = new arraylist<stops>(); public list<stops> parsexml (context context) { try { xmlpullparserfactory factory = xmlpullparserfactory.newinstance(); factory.setnamespaceaware(true); xmlpullparser xpp = factory.newpullparser(); inputstream stream = context.getresources().openrawresource(r.raw.stops_xml); xpp.setinput(stream,null); int eventtype = xpp.geteventtype(); while (eventtype != xmlpullparser.end_document) { if (eventtype == xmlpullparser.start_tag) { handlestarttag(xpp.getname()); } else if (eventtype == xmlpullparser.end_tag) { currenttag = null; } else if (eventtype == xmlpullparser.text) { handletext(xpp.gettext()); } eventtype = xpp.next(); } } catch (resources.notfoundexception e) { log.d(mainactivity.tag, e.getmessage()); } catch (xmlpullparserexception e) { log.d(mainactivity.tag, e.getmessage()); } catch (ioexception e) { log.d(mainactivity.tag, e.getmessage()); } return stops; } private void handletext(string text) { string xmltext = text; if (currentstop != null && currenttag != null) { if (currenttag.equals(stop_name)) { currentstop.setname(xmltext); } else if (currenttag.equals(stop_lat)) { currentstop.setlat(xmltext); } else if (currenttag.equals(stop_lon)) { currentstop.setlon(xmltext); } else if (currenttag.equals(number)) { currentstop.setnumber(xmltext); } } } private void handlestarttag(string name) { if (name.equals("stop")) { currentstop = new stops(); stops.add(currentstop); } else { currenttag = name; } } }
when run returns stop number of 9998 5882 entries (that correct number of entries in file). thing obvious i'm missing?
myxmlpullparser objmyxmlpullparser = new myxmlpullparser(context); list<map<string , string>> list = objmyxmlpullparser.readxml("xml respose put here", "table"); public class myxmlpullparser { context _context ; public myxmlpullparser(context _context) { this._context = _context ; } public list<map<string , string>> readxml(string xmlstring , string parenttag) { map<string , string > map = new hashmap<string, string>(); list<map<string , string >> list = new arraylist<map<string , string >>(); try { string tag = "" ; xmlpullparserfactory factory = xmlpullparserfactory.newinstance(); factory.setnamespaceaware(true); xmlpullparser xpp = factory.newpullparser(); xpp.setinput(new stringreader (xmlstring)); int eventtype = xpp.geteventtype(); while (true) { if(eventtype == xmlpullparser.start_tag) { tag = xpp.getname(); } else if(eventtype == xmlpullparser.end_tag) { tag = "" ; if(xpp.getname().equals(parenttag)) { list.add(map); map = new hashmap<string, string>(); } } else if(eventtype == xmlpullparser.text) { string text = xpp.gettext(); if(!tag.equals("") && !tag.equals(parenttag)) { map.put(tag, text); } } else if(eventtype == xmlpullparser.end_document) { system.out.println("end document"); break ; } eventtype = xpp.next(); } } catch (xmlpullparserexception e) { log.e("xml reader" , "error in parsing xml"); return null ; } catch (ioexception e) { log.e("xml reader" , "error in io in xml"); return null ; } return list ; }
hope may helps you
Comments
Post a Comment