Parsing XML with Android -
i'm developing android 2.2 application.
i have following xml file on res/xml:
<?xml version="1.0" encoding="iso-8859-1"?> <data> <turo> <name>rodo</name> <latitude>37.212123</latitude> <longitude>0.1231231</longitude> </turo> </data>
i'm getting crazy because can't find example see how can parse file.
i have following class store data retrieved:
public class turo{ private string name; private location location; public string getname(){ return name; } public void setname(string name){ this.name = name; } public turo(string name){ setname(name); } public void setlocation(location location) { this.location = location; } public location getlocation() { return location; } }
this method parse (it's incompleted):
public vector<turo> getturosfromxml(activity activity, int xmlid) throws xmlpullparserexception, ioexception { stringbuffer stringbuffer = new stringbuffer(); resources res = activity.getresources(); xmlresourceparser xpp = res.getxml(xmlid); xpp.next(); int eventtype = xpp.geteventtype(); while (eventtype != xmlpullparser.end_document) { ... } }
can me please?
you can try example:
package com.exercise.androidxmlresource; import java.io.ioexception; import org.xmlpull.v1.xmlpullparser; import org.xmlpull.v1.xmlpullparserexception; import android.app.activity; import android.content.res.resources; import android.content.res.xmlresourceparser; import android.os.bundle; import android.widget.textview; public class androidxmlresource extends activity { /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); textview myxmlcontent = (textview)findviewbyid(r.id.my_xml); string stringxmlcontent; try { stringxmlcontent = geteventsfromanxml(this); myxmlcontent.settext(stringxmlcontent); } catch (xmlpullparserexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } private string geteventsfromanxml(activity activity) throws xmlpullparserexception, ioexception { stringbuffer stringbuffer = new stringbuffer(); resources res = activity.getresources(); xmlresourceparser xpp = res.getxml(r.xml.myxml); xpp.next(); int eventtype = xpp.geteventtype(); while (eventtype != xmlpullparser.end_document) { if(eventtype == xmlpullparser.start_document) { stringbuffer.append("--- start xml ---"); } else if(eventtype == xmlpullparser.start_tag) { stringbuffer.append("\nstart_tag: "+xpp.getname()); } else if(eventtype == xmlpullparser.end_tag) { stringbuffer.append("\nend_tag: "+xpp.getname()); } else if(eventtype == xmlpullparser.text) { stringbuffer.append("\ntext: "+xpp.gettext()); } eventtype = xpp.next(); } stringbuffer.append("\n--- end xml ---"); return stringbuffer.tostring(); }
}
the android ui xml file be:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/my_xml" /> </linearlayout>
and xml
parsed in example one:
<?xml version="1.0" encoding="utf-8"?> <rootelement1> <subelement> hello xml sub-element 1 </subelement> <subelement> hello xml sub-element 2 <subsubelement>sub sub element</subsubelement> </subelement> </rootelement1>
note: info extracted android-er
Comments
Post a Comment