package cn.llbb.testpullxml;import android.support.v7.app.actionbaractivity;import android.util.log;import java.io.ioexception;import java.io.stringreader;import org.apache.http.httpentity;import org.apache.http.httpresponse;import org.apache.http.client.clientprotocolexception;import org.apache.http.client.httpclient;import org.apache.http.client.methods.httpget;import org.apache.http.impl.client.defaulthttpclient;import org.apache.http.util.entityutils;import org.xmlpull.v1.xmlpullparser;import org.xmlpull.v1.xmlpullparserexception;import org.xmlpull.v1.xmlpullparserfactory;import android.os.bundle;import android.view.*;import android.view.view.*;import android.widget.*; public class mainactivity extends actionbaractivity implements onclicklistener{ Private button btn_send = null;protected void oncreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);btn_send = (Button ) findviewbyid (r.id.btn_send); Btn_send.setonclicklistener (this);} Public void onclick (view v) {switch (V.getid ()) {case r.id.btn_send: Sendrequestwithhttpclient (); break;default:break;}} Private void sendrequestwithhttpclient () {New thread (new runnable () {public void Run () {try {httpclient client = new defaulthttpclient (); Httpget httpget = new httpget ("Http://192.168.30.77:8000/static/css/text.xml"); Httpresponse response = client.execute (HttpGet); if (Response.getstatusline (). GetStatusCode () == 200) {httpentity entity = response.getentity (); String response_txt = entityutils.tostring (Entity, "utf-8"); LOG.D ("Response", "get response successed");p arsexmlwithpull (response_txt);} ELSE{LOG.D ("Response", "Get rEsponse failed ");}} catch (clientprotocolexception e) {e.printstacktrace ();} catch (ioexception e) {e.printstacktrace ();}}). Start ();} Private void parsexmlwithpull (string xmldata) {xmlpullparserfactory factory;try { Factory = xmlpullparserfactory.newinstance (); Xmlpullparser xmlpullparser = factory.newpullparser (); Xmlpullparser.setinput (new StringReader (XMLData)); Int eventtype = xmlpullparser.geteventtype (); string id = ""; string name = ""; string version = ""; while (Eventtype != xmlpullparser. End_document) {string nodename = xmlpullparser.getname (); switch (eventtype) {case XMLPULLPARSER.START_TAG:IF ("id". Equals (NodeName)) {Try {id = xmlpullparser.nexttext ();} catch (ioexception e) {// TODO Auto-generated catch Blocke.printstacktrace ();}} Else iF ("name". Equals (NodeName)) {Try {name = xmlpullparser.nexttext ();} catch (ioexception e) {e.printstacktrace ();}} Else if ("Version". Equals (NodeName)) {Try {version = xmlpullparser.nexttext ();} catch (ioexception e) {e.printstacktrace ();}} Break;case xmlpullparser.end_tag:if ("App". Equals (NodeName)) {log.d ("id = ", id); LOG.D ("name = ", name); LOG.D ("version = ", version);} break;} Try {eventtype = xmlpullparser.next ();} catch (ioexception e) {e.printstacktrace ();}} catch (xmlpullparserexception e) {e.printstacktrace ();}}
This article from "Leboit" blog, declined reprint!
Using Xmlpullparser to parse a network XML file in Android