Pull解析本地的xml文件

来源:互联网 发布:ubuntu安装eclipse c 编辑:程序博客网 时间:2024/05/21 17:48

本地的待解析的xml文件处于项目的assets目录下,Android Studio在项目上新建Folder下的Assets folder文件,即可.

  • 解析步骤
  • 源码
    • weatherxml
    • Channeljava
    • MainActivityjava
    • WeatherParserjava
    • activity_mainxml
    • 实现截图

解析步骤

  • 获取XmlPullparser
    XmlPullParser parser = Xml.newPullParser();

  • 设置XmlPullParser 的参数
    parser.setInput(in,”utf-8”);

  • 获取解析的文档的事件类型
    int type =parser.getEventType();

  • 具体判断是那个标签

源码

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>35°</temp>        <wind>2</wind>        <pm250>100</pm250>    </channel>    <channel id="3">        <city>天津</city>        <temp>25°</temp>        <wind>2</wind>        <pm250>200</pm250>    </channel>    <channel id="3">        <city>深圳</city>        <temp>23°</temp>        <wind>4</wind>        <pm250>90</pm250>    </channel></weather>

Channel.java

package com.peng.xml2;/** * Created by Peng on 2016/7/26. */public class Channel {    private String id;    private String city;    private String temp;    private String wind;    private String pm250;    @Override    public String toString() {        return "Channel{" +                "city='" + city + '\'' +                ", id='" + id + '\'' +                ", temp='" + temp + '\'' +                ", wind='" + wind + '\'' +                ", pm250='" + 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;    }}

MainActivity.java

package com.peng.xml2;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.widget.TextView;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);        TextView tv_weather = (TextView) findViewById(R.id.tv_weather);        try {            //获取资产的管理者 通过上下文            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上            tv_weather.setText(sb.toString());        } catch (Exception e) {            e.printStackTrace();        }    }}

WeatherParser.java

package com.peng.xml2;import android.util.Xml;import org.xmlpull.v1.XmlPullParser;import java.io.InputStream;import java.util.ArrayList;import java.util.List;/** * Created by Peng on 2016/7/26. * 服务器以流的形式把数据返回 */public class WeatherParser {    public static List<Channel> parserXml(InputStream inputStream) throws Exception {        //申明集合对象        List<Channel> weatherLists = null;        Channel channel = null;        //回去Xmlpullparser 解析的实例        XmlPullParser parser = Xml.newPullParser();        //设置XmlPullParser 的参数        parser.setInput(inputStream, "utf-8");        //获取事件类型        int type = parser.getEventType();        while (type != XmlPullParser.END_DOCUMENT) {            switch (type) {                case XmlPullParser.START_TAG://解析开始标签                    //具体判断一下,解析到那个节点                    if ("weather".equals(parser.getName())) {                        //创建一个集合对象                        weatherLists = new ArrayList<Channel>();                    } else if ("channel".equals(parser.getName())) {                        //创建Channel对象                        channel = new Channel();                        //获取id值                        String id = parser.getAttributeValue(0);                        channel.setId(id);                    } else if ("city".equals(parser.getName())) {                        //获取id值                        String city = parser.nextText();                        channel.setCity(city);                    } else if ("temp".equals(parser.getName())) {                        //获取id值                        String temp = parser.nextText();                        channel.setTemp(temp);                    } else if ("wind".equals(parser.getName())) {                        //获取id值                        String wind = parser.nextText();                        channel.setWind(wind);                    } else if ("pm250".equals(parser.getName())) {                        //获取id值                        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();        }        return weatherLists;    }}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.peng.xml2.MainActivity">    <TextView        android:id="@+id/tv_weather"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Hello World!" /></RelativeLayout>

实现截图

软件截图

0 0
原创粉丝点击