Android 解析Xml和Json

解析Xml
1.找到xml文件,可以看到weather1.xml的返回值是一个I流对象

InputStream inputStream = getResources().openRawResource(R.raw.weather1);

2.解析过程看起来很麻烦,不是很懂

List<xweatherinfo> xweatherinfos=null;
   xweatherinfo xweatherinfo=null;
    //获取pull解析类
    XmlPullParser parser= Xml.newPullParser();
    //初始化解析器,告诉解析器要解析的xml文件
    parser.setInput(is,"utf-8");

    //得到当前的事件类型
    int eventType = parser.getEventType();
    while(eventType!=XmlPullParser.END_DOCUMENT)
    {
        //具体判断一下解析的是开始标签还是结束
        switch (eventType)
        {
            case XmlPullParser.START_TAG://解析的是开始标签
                if("infos".equals(parser.getName()))
                {
                    xweatherinfos=new ArrayList<xweatherinfo>();
                }else if("city".equals(parser.getName()))
                {
                    xweatherinfo =new xweatherinfo();
                    String id=parser.getAttributeValue(0);
                    xweatherinfo.setId(id);
                }else if("temp".equals(parser.getName()))
                {
                    String temp=parser.nextText();
                    xweatherinfo.setTemp(temp);
                }else if("weather".equals(parser.getName()))
                {
                    String weather=parser.nextText();
                    xweatherinfo.setTemp(weather);
                }else if("name".equals(parser.getName()))
                {
                    String name=parser.nextText();
                    xweatherinfo.setTemp(name);
                }else if("wind".equals(parser.getName()))
                {
                    String wind=parser.nextText();
                    xweatherinfo.setTemp(wind);
                }

                break;

            case XmlPullParser.END_TAG:
                if("city".equals(parser.getName()))
                {
                    xweatherinfos.add(xweatherinfo);
                }
                break;
        }
        eventType=parser.next();
    }
    return xweatherinfos;

3.xml文件

<?xml version="1.0" encoding="utf-8"?>
<infos>
      <city id="sh">
        <temp>20</temp>
        <weather>晴天</weather>
        <name>上海</name>
        <wind>1</wind>

    </city>
    <city id="bj">
        <temp>20</temp>
        <weather>阴天</weather>
        <name>北京</name>
        <wind>3</wind>

    </city>

    <city id="gz">
        <temp>20</temp>
        <weather>多囤天</weather>
        <name>广州</name>
        <wind>1</wind>

    </city>


</infos>

解析Json

这里使用Gson类库来进行解析

记一下导入方法
找到这个
在这里插入图片描述
在这里插入图片描述
然后安装第一个google什么的,然后可能会报错,我第一次就是这样,然后我又点了一下那个报错的,等了很久就直接好了

过程

 [
  {"temp": "20℃/30℃","weather": "晴转多云","name": "上海","wind": "1级"},
  {"temp": "20℃","weather": "多云","name": "北京","wind": "3级"},
  {"temp": "22℃","weather": "下雨","name": "广东","wind": "2级"}
]

首先根据temp,weather name wind 封装成一个带有get set方法的。

package com.example.demo;

public class xweatherinfo {
    private String id;
    private String temp;
    private String weather;
    private String name;
    private String wind;

    public String getId() {
        return id;
    }

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

    public String getTemp() {
        return temp;
    }

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

    public String getWeather() {
        return weather;
    }

    public void setWeather(String weather) {
        this.weather = weather;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getWind() {
        return wind;
    }

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

我们在写一个专门解析这个json对象的一个类,和方法


public static List<xweatherinfo> getinfojson(InputStream is) throws IOException {
    byte[] buff=new byte[is.available()];
    is.read(buff);
   String json= new String(buff,"utf-8");
    //使用Gson进行解析
    Gson gson=new Gson();
    Type list=new TypeToken<List<xweatherinfo>>(){}.getType();
    List<xweatherinfo> weinfo=gson.fromJson(json,list);

    return weinfo;
}

用普通的流对象,进行string化,然后使用Gson的方法

public class xml extends AppCompatActivity implements View.OnClickListener {

    private TextView c1;
    private TextView c2;
    private TextView c3;
    private List<Map<String, String>> list;
private Map<String,String> map;
    private TextView c4;

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

        //解析xml
        //InputStream inputStream = getResources().openRawResource(R.raw.weather1);
        InputStream inputStream = getResources().openRawResource(R.raw.weather2);
        try {
            //List<xweatherinfo> getinfor = xweatherservice.getinfor(inputStream);
            List<xweatherinfo> getinfor = xweatherservice.getinfojson(inputStream);
            list = new ArrayList<>();
            for (xweatherinfo info: getinfor) {
                map=new HashMap<>();
                map.put("1",info.getWeather());
                map.put("2",info.getTemp());

                map.put("3",info.getWind());
                map.put("4",info.getName());
                list.add(map);

            }


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

        //自定义一个方法getmal方法

    }


   public void getmap(int i)
    {
        Map<String,String> citymap=list.get(i);
        String temp=citymap.get("1");
        String name=citymap.get("2") ;
        String weather=citymap.get("3");
        String wind=citymap.get("4") ;
        c1.setText(name);
        c2.setText(weather);
        c3.setText(wind);
        c4.setText(temp);

    }

    private void initview() {
        c1 = findViewById(R.id.city2);
        c2 = findViewById(R.id.tianqi);
        c3 = findViewById(R.id.wnedu);
        c4 = findViewById(R.id.temp);
        findViewById(R.id.shanghai).setOnClickListener(this);
        findViewById(R.id.guangdong).setOnClickListener(this);
        findViewById(R.id.beijing).setOnClickListener(this);

    }
    @Override
    public void onClick(View v)
    {
switch (v.getId())
{
    case R.id.shanghai:
getmap(0);
        break;
    case R.id.beijing:
getmap(1);
        break;
    case R.id.guangdong:
getmap(2);
        break;
}
    }





}

更晕了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值