安卓开发之利用XmlPullParser解析XML文件

 1 package com.lidaochen.phonecall;
 2 
 3 import android.support.v7.app.AppCompatActivity;
 4 import android.os.Bundle;
 5 import android.util.Xml;
 6 import android.view.View;
 7 import android.widget.TabHost;
 8 import android.widget.TextView;
 9 import android.widget.ThemedSpinnerAdapter;
10 
11 import java.io.InputStream;
12 import java.util.ArrayList;
13 import java.util.List;
14 
15 public class MainActivity extends AppCompatActivity{
16     @Override
17     protected void onCreate(Bundle savedInstanceState) {
18         super.onCreate(savedInstanceState);
19         setContentView(R.layout.activity_main);
20         try
21         {
22             // 找到TextView控件
23             TextView textView = (TextView)findViewById(R.id.textView);
24             // 通过上下文获取资产管理者
25             InputStream inputStream = getAssets().open("weather.xml");
26             // 调用我们定义的方法解析XML文件
27             List<Channel> weatherlists = WeatherParser.parserXml(inputStream);
28             StringBuffer sb = new StringBuffer();
29             for (Channel channel:weatherlists)
30             {
31                 sb.append(channel.toString());
32             }
33             // 将解析的文件内容显示到TextView控件上面
34             textView.setText(sb.toString());
35         }
36         catch (Exception e)
37         {
38             e.printStackTrace();
39         }
40     }
41 }
 1 package com.lidaochen.phonecall;
 2 
 3 import android.util.Xml;
 4 
 5 import org.xmlpull.v1.XmlPullParser;
 6 
 7 import java.io.InputStream;
 8 import java.util.ArrayList;
 9 import java.util.List;
10 
11 public class WeatherParser {
12     // 服务器是以流的形式把数据返回的
13     public static List<Channel> parserXml(InputStream In) throws Exception
14     {
15         // 声明集合对象
16         List<Channel> weatherlists = null;
17         Channel channel = null;
18         // 获取XmlPullParser解析的实例
19         XmlPullParser xmlPullParser = Xml.newPullParser();
20         // 设置XmlPullParser的参数
21         xmlPullParser.setInput(In, "utf-8");
22         // 获取事件类型
23         int type = xmlPullParser.getEventType();
24         while(type != XmlPullParser.END_DOCUMENT)
25         {
26             switch (type)
27             {
28                 case XmlPullParser.START_TAG:
29                     if ("weather".equals(xmlPullParser.getName()))
30                     {
31                         // 创建一个集合对象
32                         weatherlists = new ArrayList<Channel>();
33                     }
34                     else if ("channel".equals(xmlPullParser.getName()))
35                     {
36                         // 创建Channel对象
37                         channel = new Channel();
38                         // 获取id值
39                         String id = xmlPullParser.getAttributeValue(0);
40                         channel.setId(id);
41                     }
42                     else if ("city".equals(xmlPullParser.getName()))
43                     {
44                         String city = xmlPullParser.nextText();
45                         channel.setCity(city);
46                     }
47                     else if ("temp".equals(xmlPullParser.getName()))
48                     {
49                         String temp = xmlPullParser.nextText();
50                         channel.setTemp(temp);
51                     }
52                     else if ("wind".equals(xmlPullParser.getName()))
53                     {
54                         String wind = xmlPullParser.nextText();
55                         channel.setWind(wind);
56                     }
57                     else if ("pm250".equals(xmlPullParser.getName()))
58                     {
59                         String pm250 = xmlPullParser.nextText();
60                         channel.setPm250(pm250);
61                     }
62                     break;
63                 case XmlPullParser.END_TAG:   // 解析结束标志
64                     // 判断要解析的结束标签是不是channel
65                     if ("channel".equals(xmlPullParser.getName()))
66                     {
67                         // 把javabean对象存到集合中
68                         weatherlists.add(channel);
69                     }
70                     break;
71             }
72             // 不停的向下解析
73             type = xmlPullParser.next();
74         }
75 
76         return weatherlists;
77     }
78 }
 1 package com.lidaochen.phonecall;
 2 
 3 public class Channel {
 4     private String id;
 5     private String city;
 6     private String temp;
 7     private String wind;
 8     private String pm250;
 9 
10     public String getId() {
11         return id;
12     }
13 
14     public void setId(String id) {
15         this.id = id;
16     }
17 
18     public String getCity() {
19         return city;
20     }
21 
22     public void setCity(String city) {
23         this.city = city;
24     }
25 
26     public String getTemp() {
27         return temp;
28     }
29 
30     public void setTemp(String temp) {
31         this.temp = temp;
32     }
33 
34     public String getWind() {
35         return wind;
36     }
37 
38     public void setWind(String wind) {
39         this.wind = wind;
40     }
41 
42     public String getPm250() {
43         return pm250;
44     }
45 
46     public void setPm250(String pm250) {
47         this.pm250 = pm250;
48     }
49 
50     @Override
51     public  String toString()
52     {
53         return "Channel [id=" + id + ", city=" + city + ", temp=" + temp
54                 + ", wind=" + wind + ", pm250=" + pm250 + "]\n";
55     }
56 }

下面是要解析的XML文件,需要放在assets目录下

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <weather>
 3         <channel id ='1'>
 4              <city>北京</city>
 5              <temp>25°</temp>
 6              <wind>3</wind>
 7              <pm250>300</pm250>
 8              
 9         </channel>
10         
11          <channel id ='2'>
12              <city>郑州</city>
13              <temp>20°</temp>
14              <wind>4</wind>
15              <pm250>300</pm250>
16              
17         </channel>
18         
19         <channel id ='3'>
20              <city>长春</city>
21              <temp>10°</temp>
22              <wind>4</wind>
23              <pm250>100</pm250>
24              
25         </channel>
26         
27         <channel id ='4'>
28              <city>沈阳</city>
29              <temp>20°</temp>
30              <wind>1</wind>
31              <pm250>50</pm250>
32         </channel>
33 
34 
35 </weather>

猜你喜欢

转载自www.cnblogs.com/duxie/p/10890465.html