XML文件解析

谈一下Android小白的xml文件解析

本文代码参考黑马程序员android授课内容

从网上下载了一个名为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>

下面就来读取此文件的内容:

将文件放在资产目录下:在main目录中新建名为assets的目录,将文件放在里面:

首先根据文件新建了一个JavaBean文件:MyJavaBean.java:

 1 package com.lgqchinese.xmlread;
 2 
 3 public class MyJavaBean {
 4     private String id;
 5     private String city;
 6     private String temp;
 7     private String wind;
 8     private String pm250;
 9 
10     public void setId(String id) {
11         this.id = id;
12     }
13 
14     public void setCity(String city) {
15         this.city = city;
16     }
17 
18     public void setTemp(String temp) {
19         this.temp = temp;
20     }
21 
22     public void setWind(String wind) {
23         this.wind = wind;
24     }
25 
26     public void setPm250(String pm250) {
27         this.pm250 = pm250;
28     }
29 
30     public String getId() {
31         return id;
32     }
33 
34     public String getCity() {
35         return city;
36     }
37 
38     public String getTemp() {
39         return temp;
40     }
41 
42     public String getWind() {
43         return wind;
44     }
45 
46     public String getPm250() {
47         return pm250;
48     }
49 
50     @Override
51     public String toString() {
52         return "MyJavaBean{" +
53                 "id='" + id + '\'' +
54                 ", city='" + city + '\'' +
55                 ", temp='" + temp + '\'' +
56                 ", wind='" + wind + '\'' +
57                 ", pm250='" + pm250 + '\'' +
58                 '}';
59     }
60 }

然后新建解析文件类来对文件进行解析:

 1 package com.lgqchinese.xmlread;
 2 
 3 import android.util.Xml;
 4 
 5 import org.xmlpull.v1.XmlPullParser;
 6 import org.xmlpull.v1.XmlPullParserException;
 7 
 8 import java.io.IOException;
 9 import java.io.InputStream;
10 import java.util.ArrayList;
11 import java.util.List;
12 
13 public class XmlReadFunc {
14     public static List<MyJavaBean> parserXML(InputStream inputStream) throws XmlPullParserException, IOException {
15         List<MyJavaBean> weatherLists = null;
16         MyJavaBean myJavaBean = null;
17         //获取实例
18         XmlPullParser pullParser = Xml.newPullParser();
19         //设置XmlPullParser参数
20         pullParser.setInput(inputStream, "utf-8");
21         //获取文档的时间类型
22         int type = pullParser.getEventType();
23         while (type != XmlPullParser.END_DOCUMENT) {
24             //判断解析到哪个节点
25             switch (type) {
26                 case XmlPullParser.START_TAG://解析开始标签
27                     //判断具体开始标签
28                     if ("weather".equals(pullParser.getName())) {
29                         weatherLists = new ArrayList<MyJavaBean>();
30                     } else if ("channel".equals(pullParser.getName())) {
31                         //创建myJavaBean对象
32                         myJavaBean = new MyJavaBean();
33                         //获取id值
34                         String id = pullParser.getAttributeValue(0);
35                         myJavaBean.setId(id);
36 
37                     } else if ("city".equals(pullParser.getName())) {
38                         //获取city内容
39                         String city = pullParser.nextText();
40                         myJavaBean.setCity(city);
41 
42                     } else if ("temp".equals(pullParser.getName())) {
43                         //获取temp内容
44                         String temp = pullParser.nextText();
45                         myJavaBean.setTemp(temp);
46 
47                     } else if ("wind".equals(pullParser.getName())) {
48                         //获取wind内容
49                         String wind = pullParser.nextText();
50                         myJavaBean.setWind(wind);
51 
52                     } else if ("pm250".equals(pullParser.getName())) {
53                         //获取pm250内容
54                         String pm250 = pullParser.nextText();
55                         myJavaBean.setPm250(pm250);
56 
57                     }
58                     break;
59                 case XmlPullParser.END_TAG://解析结束标签
60                     //判断具体的结束标签
61                     if ("channel".equals(pullParser.getName())) {
62                         //把javabean对象放入集合中
63                         weatherLists.add(myJavaBean);
64                     }
65                     break;
66             }
67 
68 
69             //不停的向下解析
70             type = pullParser.next();
71         }
72 
73 
74         return weatherLists;
75     }
76 }

MainActivity.java:

 1 package com.lgqchinese.xmlread;
 2 
 3 import android.support.v7.app.AppCompatActivity;
 4 import android.os.Bundle;
 5 import android.widget.TextView;
 6 import android.widget.Toast;
 7 
 8 import org.w3c.dom.Text;
 9 import org.xmlpull.v1.XmlPullParser;
10 import org.xmlpull.v1.XmlPullParserException;
11 
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.util.List;
15 
16 public class MainActivity extends AppCompatActivity {
17 
18     @Override
19     protected void onCreate(Bundle savedInstanceState) {
20         super.onCreate(savedInstanceState);
21         setContentView(R.layout.activity_main);
22 
23         try {
24             //显示xml数据
25             TextView textView = findViewById(R.id.tv);
26             //通过上下文获取资产管理者
27             InputStream inputStream = getAssets().open("weather.xml");
28             //调用解析方法
29             List<MyJavaBean> weatherLists = null;
30             try {
31                 weatherLists = XmlReadFunc.parserXML(inputStream);
32             } catch (XmlPullParserException e) {
33                 e.printStackTrace();
34             }
35 
36             StringBuffer sb = new StringBuffer();
37             for (MyJavaBean myJavaBean:weatherLists){
38                 sb.append(myJavaBean.toString());
39             }
40 
41             //把数据展示到控件上
42             textView.setText(sb.toString());
43         } catch (IOException e) {
44             e.printStackTrace();
45             Toast.makeText(MainActivity.this,"sidfg",Toast.LENGTH_SHORT).show();
46         }
47     }
48 }

 

posted @ 2018-10-27 17:38  龙谷情Sinoam  阅读(308)  评论(0编辑  收藏  举报
Smiley face