xml解析(使用pull解析)

1.要解析的xml文件

<?xml version="1.0" encoding="utf-8"?>
<infos>
      <city id="1">
            <temp>20℃/30℃</temp>
            <weather>5月20号 多云转阴</weather>
            <wind>南风3-4级</wind>
            <name>上海</name>
            <pm>200</pm>
      </city>
      <city id="2">
            <temp>20℃/30℃</temp>
            <weather>5月21号 多云转阴</weather>
            <wind>南风6-7级</wind>
            <name>北京</name>
            <pm>100</pm>
      </city>
      <city id="3">
            <temp>10℃/20℃</temp>
            <weather>5月20号 多云转阴</weather>
            <wind>北风3-4级</wind>
            <name>长沙</name>
            <pm>200</pm>
      </city>
</infos>

xml解析(使用pull解析)

2.写一个实体类

package com.example.testpull;

public class WeatherInfo {
    int id;
    String name;
    String wind;
    String weather;
    String temp;
    String pm;

    @Override
    public String toString() {
        return "id=" + id + ", name=" + name + ", wind=" + wind + ", weather="
                + weather + ", temp=" + temp + ", pm=" + pm;
    }

}


3.定义解析的方法

package com.example.testpull;

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

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

import android.util.Xml;

public class WeathearService {
    public static List<WeatherInfo> getWeatherInfos(InputStream is)
            throws XmlPullParserException, IOException {
        XmlPullParser parser = Xml.newPullParser();
        List<WeatherInfo> weatherInfos = null;
        WeatherInfo weatherInfo = null;
        // 初始化解析器
        parser.setInput(is, "utf-8");
        int type = parser.getEventType();// 得到事件类型
        while (type != XmlPullParser.END_DOCUMENT) {
            switch (type) {
            case XmlPullParser.START_TAG:
                if ("infos".equals(parser.getName())) {
                    weatherInfos = new ArrayList<WeatherInfo>();
                } else if ("city".equals(parser.getName())) {
                    weatherInfo = new WeatherInfo();
                    String idStr = parser.getAttributeValue(0);
                    weatherInfo.id = Integer.parseInt(idStr);
                } else if ("temp".equals(parser.getName())) {
                    String temp = parser.nextText();
                    weatherInfo.temp = temp;
                } else if ("weather".equals(parser.getName())) {
                    String weather = parser.nextText();
                    weatherInfo.weather = weather;
                } else if ("wind".equals(parser.getName())) {
                    String wind = parser.nextText();
                    weatherInfo.wind = wind;
                } else if ("name".equals(parser.getName())) {
                    String name = parser.nextText();
                    weatherInfo.name = name;
                } else if ("pm".equals(parser.getName())) {
                    String pm = parser.nextText();
                    weatherInfo.pm = pm;
                }
                break;

            case XmlPullParser.END_TAG:
                if ("city".equals(parser.getName())) {
                    // 一个城市的信息处理完毕了
                    weatherInfos.add(weatherInfo);
                    weatherInfo = null;
                }
                break;
            }
            type = parser.next();
        }
        return weatherInfos;
    }
}

4.主main

package com.example.testpull;

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

import org.xmlpull.v1.XmlPullParserException;

import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;

public class MainActivity extends Activity {
    TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.tv);

        List<WeatherInfo> infos = null;
        try {
            infos = WeathearService
                    .getWeatherInfos(MainActivity.class.getClassLoader()
                            .getResourceAsStream("weather.xml"));
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        StringBuilder sb = new StringBuilder();
        for (WeatherInfo info : infos) {
            String str = info.toString();
            sb.append(str);
            sb.append("\n");
        }
        tv.setText(sb);
    }

}


你可能感兴趣的:(xml解析(使用pull解析))