1. 程式人生 > >使用XmlPullParser解析xml檔案

使用XmlPullParser解析xml檔案

步驟:
解析類:
1、獲取XmlPullParser解析的例項
2、設定XmlPullParser的引數
3、獲取事件型別,判斷是開始標籤還是結束標籤
4、具體判斷一下解析到哪個開始標籤
5、建立集合物件
6、建立解析物件
7、獲取解析物件內的各標籤的資料並儲存到解析物件中
8、將解析物件的資料儲存到集合中

主視窗類:
1、例項化控制元件
2、獲取資產的管理者
3、呼叫解析類的業務方法
4、將資訊展示到安卓控制元件上

專案目錄結構
在這裡插入圖片描述

Channel類儲存變數,程式碼如下:

package com.example.xmlparser;

public class Channel {
    private String id;
    private String city;
    private String temp;
    private String wind;
    private String pm250;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getTemp() {
        return temp;
    }

    public void setTemp(String temp) {
        this.temp = temp;
    }

    public String getWind() {
        return wind;
    }

    public void setWind(String wind) {
        this.wind = wind;
    }

    public String getPm250() {
        return pm250;
    }

    public void setPm250(String pm250) {
        this.pm250 = pm250;
    }
    public String toString(){
        return "Channel[id="+id+",city="+city+",temp="+temp
                +",wind="+wind+",pm250="+pm250+"]";
    }
}

WeatherParser解析xml:程式碼如下:

package com.example.xmlparser;

import android.util.Xml;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class WeatherParser {
    /**
     *
     * 伺服器是以流的形式把資料返回的
     * @return
     */
    public static List<Channel> parserXml(InputStream in){
        //[0]宣告集合物件
        List<Channel> weatherLists=null;
        Channel channel=null;
        //[1]獲取XmlPullParser解析的例項
        XmlPullParser parser= Xml.newPullParser();


        try {
            //[2]設定XmlPullParser的引數
            parser.setInput(in,"utf-8");

            //[3]獲取事件型別
            int type=parser.getEventType();
            while (type!=XmlPullParser.END_DOCUMENT){

                switch (type){
                    case XmlPullParser.START_TAG://解析開始標籤
                        //[4]具體判斷一下解析到了哪個開始標誌
                        if ("weather".equals(parser.getName())){
                            //[5]建立一個集合物件
                            weatherLists=new ArrayList<Channel>();

                        }else if ("channel".equals(parser.getName())){
                            //[6]建立channel物件
                            channel=new Channel();

                            //[7]獲取id值
                            String id=parser.getAttributeValue(0);
                            channel.setId(id);

                        }else if ("city".equals(parser.getName())){
                            //[8]獲取city的資料
                            String city=parser.nextText();
                            channel.setCity(city);

                        }else if ("temp".equals(parser.getName())){
                            //[8]獲取temp的資料
                            String temp=parser.nextText();
                            channel.setTemp(temp);

                        }else if ("wind".equals(parser.getName())){
                            //[8]獲取wind的資料
                            String wind=parser.nextText();
                            channel.setWind(wind);

                        }else if ("pm250".equals(parser.getName())){
                            //[8]獲取pm250的資料
                            String pm250=parser.nextText();
                            channel.setPm250(pm250);

                        }

                        break;

                    case XmlPullParser.END_TAG://解析結束標籤
                        //判斷以下要解析的結束標籤
                        if ("channel".equals(parser.getName())){
                            //把javabean物件存到集合中
                            weatherLists.add(channel);

                        }
                        break;
                }

                //不停的向下解析
                type=parser.next();

            }


        } catch (Exception e) {
            e.printStackTrace();
        }


        return weatherLists;
    }
}

主視窗類將解析出的資訊顯示大安卓介面

package com.example.xmlparser;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class MainActivity extends AppCompatActivity {

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


        try {
            //[1]顯示天氣資訊
            TextView tv_weather=findViewById(R.id.tv_weather);
            //[1.1]獲取資產的管理者 通過上下文
            InputStream inputStream=getAssets().open("weather.xml");
            //[2]呼叫我們定義的解析xml業務方法
            List<Channel> weatherlists=WeatherParser.parserXml(inputStream);
            StringBuffer sb=new StringBuffer();
            for (Channel channel:weatherlists){
                sb.append(channel.toString());
            }

            //[3]那資料展示到textView上
            tv_weather.setText(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }



    }
}