xml parsing - Noob question about Android and XmlParser -
i'm new android want read xml-file , write content listview
the xml parser must problem. hope little mistake in code.
orderxml2.xml
<?xml version="1.0"?> <order> <item id="1"> <title>bitest1</title> <group>g1</group> <price>5.00</price> <description>example</description> </item> <item id="2"> <title>test2</title> <group>g1</group> <price>2.00</price> <description>example</description> </item> </order> the parser
public class order extends activity { private string[] lv_arr = {}; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.order); try { arraylist<string> orderxmlarray = preparelistfromxml(); lv_arr = (string[]) orderxmlarray.toarray(new string[0]); } catch (xmlpullparserexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } listview lv = (listview) findviewbyid(r.id.listvieworder1); lv.setadapter(new arrayadapter<string>(order.this, android.r.layout.simple_list_item_1, lv_arr)); } public arraylist<string> preparelistfromxml() throws xmlpullparserexception, ioexception { arraylist<string> orderitems = new arraylist<string>(); xmlresourceparser orderxml = getresources().getxml(r.xml.orderxml2); orderxml.next(); int eventtype = orderxml.geteventtype(); while (eventtype != xmlresourceparser.end_document) { if (eventtype == xmlresourceparser.start_document) { orderitems.add(orderxml.getattributevalue(null, "title")); } else if(eventtype == xmlresourceparser.end_document) { orderitems.add(orderxml.getattributevalue(null, "title")); } try { eventtype = orderxml.next(); } catch (xmlpullparserexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } return orderitems; } }
i think, can visit site http://www.ibm.com/developerworks/opensource/library/x-android/ . read section easier sax parsing, think solution presented there better have right know, less error prone.
Comments
Post a Comment