Pull方法解析XML

package com.zq.xmltest;

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.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    sendRequestWithHttpClient();
}

private void sendRequestWithHttpClient() {
    // TODO Auto-generated method stub
    new Thread(new Runnable(){

        @Override
        public void run() {
            // TODO Auto-generated method stub

            try {
                HttpClient httpClient = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet("http://192.168.1.4/test/get_data.xml");
                HttpResponse httpResponse = httpClient.execute(httpGet);
                if(httpResponse.getStatusLine().getStatusCode() == 200)
                {
                    HttpEntity entity = httpResponse.getEntity();
                    String response = EntityUtils.toString(entity, "utf-8");

                    parseXMLWithPull(response);

                }

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        }).start();
}

private void parseXMLWithPull(String xmlData) {
    // TODO Auto-generated method stub
    try {
        XmlPullParserFactory 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:
                   try {
                     if("id".equals(nodeName))
                     {
                        id = xmlPullParser.nextText();
                     }
                     else if("name".equals(nodeName))
                     {
                         name = xmlPullParser.nextText();
                     }
                     else if("version".equals(nodeName))
                     {
                         version = xmlPullParser.nextText();
                     }
                     break;
                   } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
               case XmlPullParser.END_TAG:
               {
                   if("app".equals(nodeName))
                   {
                       Log.d("ZQ", "id is " + id );
                       Log.d("ZQ", "name is " + name);
                       Log.d("ZQ", "version is " + version);
                   }
                   break;
               }
               default:
                   break;
            }
            try {
                eventType = xmlPullParser.next();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }


    } catch (XmlPullParserException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值