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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值