Using Xmlpullparser to parse a network XML file in Android

Source: Internet
Author: User

package cn.llbb.testpullxml;import;import  android.util.log;import;import;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 (; Btn_send.setonclicklistener (this);} Public void onclick (view v)  {switch (V.getid ()) {case Sendrequestwithhttpclient (); break;default:break;}} Private void sendrequestwithhttpclient () {New thread (new runnable () {public void  Run ()  {try {httpclient client = new defaulthttpclient (); Httpget httpget = new httpget ("Http://"); 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 = ();}  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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.