Android XmlPullParser解析XML文件

1、創建一個weather.xml文件

<?xml version="1.0" encoding="utf-8"?>
<weather>
    <channel id ='1'>
            <city>北京</city>
            <temp>25°</temp>
            <wind>3</wind>
            <pm250>300</pm250>

    </channel>

    <channel id ='2'>
        <city>鄭州</city>
        <temp>20°</temp>
        <wind>4</wind>
        <pm250>300</pm250>

    </channel>

    <channel id ='3'>
        <city>長春</city>
        <temp>10°</temp>
        <wind>4</wind>
        <pm250>100</pm250>

    </channel>

    <channel id ='4'>
        <city>瀋陽</city>
        <temp>20°</temp>
        <wind>1</wind>
        <pm250>50</pm250>
    </channel>


</weather>

2、創建一個實體類Channel.java

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;
    }

    @Override
    public String toString() {
        return "Channel{" +
                "id='" + id + '\'' +
                ", city='" + city + '\'' +
                ", temp='" + temp + '\'' +
                ", wind='" + wind + '\'' +
                ", pm250='" + pm250 + '\'' +
                '}';
    }
}

3、創建一個解析器WeatherParser.java

public class WeatherParser {
    // 服務器是以流的形式把數據返回的
    public static List<Channel> parserXml(InputStream In) throws Exception {
        // 聲明集合對象
        List<Channel> weatherlists = null;
        Channel channel = null;
        // 獲取XmlPullParser解析的實例
        XmlPullParser xmlPullParser = Xml.newPullParser();
        // 設置XmlPullParser的參數
        xmlPullParser.setInput(In, "utf-8");
        // 獲取事件類型
        int type = xmlPullParser.getEventType();
        while (type != XmlPullParser.END_DOCUMENT) {
            switch (type) {
                case XmlPullParser.START_TAG:
                    if ("weather".equals(xmlPullParser.getName())) {
                        // 創建一個集合對象
                        weatherlists = new ArrayList<Channel>();
                    } else if ("channel".equals(xmlPullParser.getName())) {
                        // 創建Channel對象
                        channel = new Channel();
                        // 獲取id值
                        String id = xmlPullParser.getAttributeValue(0);
                        channel.setId(id);
                    } else if ("city".equals(xmlPullParser.getName())) {
                        String city = xmlPullParser.nextText();
                        channel.setCity(city);
                    } else if ("temp".equals(xmlPullParser.getName())) {
                        String temp = xmlPullParser.nextText();
                        channel.setTemp(temp);
                    } else if ("wind".equals(xmlPullParser.getName())) {
                        String wind = xmlPullParser.nextText();
                        channel.setWind(wind);
                    } else if ("pm250".equals(xmlPullParser.getName())) {
                        String pm250 = xmlPullParser.nextText();
                        channel.setPm250(pm250);
                    }
                    break;
                case XmlPullParser.END_TAG:   // 解析結束標誌
                    // 判斷要解析的結束標籤是不是channel
                    if ("channel".equals(xmlPullParser.getName())) {
                        // 把javabean對象存到集合中
                        weatherlists.add(channel);
                    }
                    break;
            }
            // 不停的向下解析
            type = xmlPullParser.next();
        }

        return weatherlists;
    }
}

4、把需要解析的xml文件內容顯示出來

ublic class MainActivity extends AppCompatActivity {

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

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

        // Example of a call to a native method
        try {
            // 找到TextView控件
            TextView textView = (TextView) findViewById(R.id.sample_text);
            // 通過上下文獲取資產管理者
            InputStream inputStream = getAssets().open("weather.xml");
            // 調用我們定義的方法解析XML文件
            List<Channel> weatherlists = WeatherParser.parserXml(inputStream);
            StringBuffer sb = new StringBuffer();
            for (Channel channel : weatherlists) {
                sb.append(channel.toString());
            }
            // 將解析的文件內容顯示到TextView控件上面
            textView.setText(sb.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();
}

結果如下所示

2020-04-14 23:06:53.050 32420-32420/? D/wq892373445: Channel{id='1', city='北京', temp='25°', wind='3', pm250='300'}Channel{id='2', city='鄭州', tem
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章