网络xml解析

主类:

package com.example.test2xml;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Xml;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity {
    String path = "http://www.oschina.net/action/api/news_list";

    Handler handler = new Handler() {

        public void handleMessage(android.os.Message msg) {
            String xml = (String) msg.obj;

            xml(xml);
        };
    };

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

        HttpUtils httpUtils = new HttpUtils();

        httpUtils.send(HttpMethod.GET, path,
                new RequestCallBack() {

                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void onSuccess(ResponseInfo arg0) {
                        System.out.println(arg0.result);
                        Message msg = Message.obtain();
                        msg.obj = arg0.result;
                        handler.sendMessage(msg);
                    }
                });

    }

    protected void xml(String xml) {
        XmlPullParser newPullParser = Xml.newPullParser();

        InputStream is = new ByteArrayInputStream(xml.getBytes());
        try {
            newPullParser.setInput(new InputStreamReader(is));

            int eventType = newPullParser.getEventType();

            while (eventType != XmlPullParser.END_DOCUMENT) {

                switch (eventType) {

                case XmlPullParser.START_DOCUMENT:
                    System.out.println("文档开始");
                    break;

                case XmlPullParser.START_TAG:
                    String tagName = newPullParser.getName();
                    if(tagName.equals("title")){

                        String nextText = newPullParser.nextText();
                        System.out.println(nextText);
                    }else if(tagName.equals("body")){
                        String nextText = newPullParser.nextText();
                        System.out.println(nextText);

                    }

                    break;

                case XmlPullParser.END_TAG:
                    // 得到结束标签的名字
                    tagName = newPullParser.getName();
                    break;
                }

                eventType = newPullParser.next();
            }

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

    }

}

你可能感兴趣的:(网络xml解析)