Webserver to android XML parsing -
my dot-net web server returning xml. want show data in android app.
how proceed?
i have soap class gets xml response , returns response. how parse response in list view?
public string getalldetails() { strictmode.threadpolicy policy = new strictmode.threadpolicy.builder() .permitall().build(); strictmode.setthreadpolicy(policy); // create envelop.envelop used send request soapserializationenvelope envelope = new soapserializationenvelope( soapenvelope.ver11); envelope.setoutputsoapobject(request); // says soap webservice .net service envelope.dotnet = true; httptransportse androidhttptransport = new httptransportse(url); string s = ""; androidhttptransport.debug=true; androidhttptransport.setxmlversiontag("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); try { androidhttptransport.call(soap_action, envelope); soapprimitive response = (soapprimitive) envelope.getresponse(); s = response.tostring(); log.d("converter", response.tostring()); } catch (exception e) { e.printstacktrace(); } return s; }
you need create xmlparser.java file enable parse xml. use respective methods parse through response xml output , show them either listview, gridview, cardview etc. refer code below, works me when comes parsing xml.
//-------------------------------------------------------------- //** xml output //-------------------------------------------------------------- <?xml version="1.0" encoding="utf-8"?> <menu> <item> <id>1</id> <name>manchester united</name> </item> <item> <id>2</id> <name>barcelona</name> </item> <item> <id>3</id> <name>real madrid</name> </item> <item> <id>4</id> <name>arsenal</name> </item> </menu> //-------------------------------------------------------------- //** list_item.xml //-------------------------------------------------------------- <?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <textview android:id="@+id/name" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textcolor="#dc6800" android:textsize="16sp" android:textstyle="bold" android:paddingtop="6dip" android:paddingbottom="2dip" /> </linearlayout> </linearlayout> //-------------------------------------------------------------- //** main.xml //-------------------------------------------------------------- <?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <listview android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </linearlayout> //-------------------------------------------------------------- //** xmlparser.java //-------------------------------------------------------------- public class xmlparser { // constructor public xmlparser() { } /** * getting xml url making http request * @param url string * */ public string getxmlfromurl(string url) { string xml = null; try { // defaulthttpclient defaulthttpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); httpresponse httpresponse = httpclient.execute(httppost); httpentity httpentity = httpresponse.getentity(); xml = entityutils.tostring(httpentity); } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } // return xml return xml; } /** * getting xml dom element * @param xml string * */ public document getdomelement(string xml){ document doc = null; documentbuilderfactory dbf = documentbuilderfactory.newinstance(); try { documentbuilder db = dbf.newdocumentbuilder(); inputsource = new inputsource(); is.setcharacterstream(new stringreader(xml)); doc = db.parse(is); } catch (parserconfigurationexception e) { log.e("error: ", e.getmessage()); return null; } catch (saxexception e) { log.e("error: ", e.getmessage()); return null; } catch (ioexception e) { log.e("error: ", e.getmessage()); return null; } return doc; } /** getting node value * @param elem element */ public final string getelementvalue( node elem ) { node child; if( elem != null){ if (elem.haschildnodes()){ for( child = elem.getfirstchild(); child != null; child = child.getnextsibling() ){ if( child.getnodetype() == node.text_node ){ return child.getnodevalue(); } } } } return ""; } /** * getting node value * @param element node * @param key string * */ public string getvalue(element item, string str) { nodelist n = item.getelementsbytagname(str); return this.getelementvalue(n.item(0)); } } //-------------------------------------------------------------- //** mainactivity.java //-------------------------------------------------------------- public class mainactivity extends listactivity { // static variables static final string url = "http://api.foo.foo/foo/?format=xml"; // xml output // xml node keys static final string key_item = "item"; // parent node static final string key_id = "id"; static final string key_name = "name"; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); arraylist<hashmap<string, string>> menuitems = new arraylist<hashmap<string, string>>(); xmlparser parser = new xmlparser(); string xml = parser.getxmlfromurl(url); // getting xml document doc = parser.getdomelement(xml); // getting dom element nodelist nl = doc.getelementsbytagname(key_item); // looping through item nodes <item> (int = 0; < nl.getlength(); i++) { // creating new hashmap hashmap<string, string> map = new hashmap<string, string>(); element e = (element) nl.item(i); // adding each child node hashmap key => value map.put(key_id, parser.getvalue(e, key_id)); map.put(key_name, parser.getvalue(e, key_name)); // adding hashlist arraylist menuitems.add(map); } // adding menuitems listview listadapter adapter = new simpleadapter(this, menuitems, r.layout.list_item, new string[] { key_name }, new int[] { r.id.name }); setlistadapter(adapter); // selecting single listview item listview lv = getlistview(); lv.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { // getting values selected listitem string name = ((textview) view.findviewbyid(r.id.name)).gettext().tostring(); } }); } }
Comments
Post a Comment