对xml进行解析

1、要解析的xml文件

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <infos>
 3     <city id="1">
 4         <temp>20C/30C</temp>
 5         <weather>多云转晴</weather>
 6         <wind>7-8级</wind>
 7         <name>广州</name>
 8         <pm>200</pm>
 9     </city>
10     <city id="2">
11         <temp>25C/30C</temp>
12         <weather>多云转晴</weather>
13         <wind>2-3级</wind>
14         <name>钦州</name>
15         <pm>100</pm>
16     </city>
17     <city id="3">
18         <temp>20C/30C</temp>
19         <weather>多云转晴</weather>
20         <wind>7-9级</wind>
21         <name>北海</name>
22         <pm>250</pm>
23     </city>
24 </infos>

2、activity代码

 1 package com.example.myweather;
 2 
 3 import java.util.List;
 4 
 5 import com.example.myweather.service.WeatherService;
 6 import com.example.myweather.domain.WeatherInfo;
 7 import android.os.Bundle;
 8 import android.app.Activity;
 9 import android.view.Menu;
10 import android.widget.TextView;
11 import android.widget.Toast;
12 
13 public class MainActivity extends Activity {
14 
15     @Override
16     protected void onCreate(Bundle savedInstanceState) {
17         super.onCreate(savedInstanceState);
18         setContentView(R.layout.activity_main);
19         
20         TextView tv = (TextView)findViewById(R.id.tv);
21         try{
22             List<WeatherInfo> Infos = WeatherService.getWeatherInfos(MainActivity.class.getClassLoader().getResourceAsStream("weather.xml"));
23             StringBuffer sb = new StringBuffer();
24             for(WeatherInfo info : Infos){
25                 String str = info.toString();
26                 sb.append(str);
27                 sb.append("\n");
28             }
29             
30             tv.setText(sb.toString());
31         }catch(Exception e){
32             e.printStackTrace();
33             Toast.makeText(this, "解析失败!", 0).show();
34         }
35         
36     }
37 
38 }

3、解析xml

 1 package com.example.myweather.service;
 2 
 3 import java.io.InputStream;
 4 import java.util.ArrayList;
 5 import java.util.List;
 6 
 7 import org.xmlpull.v1.XmlPullParser;
 8 import org.xmlpull.v1.XmlPullParserException;
 9 
10 import android.util.Xml; 
11 
12 import com.example.myweather.domain.WeatherInfo;
13 
14 public class WeatherService {
15 
16     public static List<WeatherInfo> getWeatherInfos(InputStream is) throws Exception{
17         XmlPullParser parser = Xml.newPullParser();
18         parser.setInput(is,"utf-8");
19         List<WeatherInfo> weatherInfos = null;
20         WeatherInfo weatherInfo = null;
21         int type = parser.getEventType();
22         while(type != XmlPullParser.END_DOCUMENT){
23             
24             switch(type){
25             case XmlPullParser.START_TAG:
26                 if("infos".equals(parser.getName())){
27                     weatherInfos = new ArrayList<WeatherInfo>();
28                 }else if("city".equals(parser.getName())){
29                     weatherInfo = new WeatherInfo();
30                     String idStr = parser.getAttributeValue(0);
31                     weatherInfo.setId(Integer.parseInt(idStr));
32                 }else if("temp".equals(parser.getName())){
33                     String temp = parser.nextText();
34                     weatherInfo.setTemp(temp);
35                 }else if("weather".equals(parser.getName())){
36                     String weather = parser.nextText();
37                     weatherInfo.setWeather(weather);
38                 }else if("wind".equals(parser.getName())){
39                     String wind = parser.nextText();
40                     weatherInfo.setWind(wind);
41                 }else if("name".equals(parser.getName())){
42                     String name = parser.nextText();
43                     weatherInfo.setName(name);
44                 }else if("pm".equals(parser.getName())){
45                     String pm = parser.nextText();
46                     weatherInfo.setPm(pm);
47                 }
48                 break;
49             case XmlPullParser.END_TAG:
50                 if("city".equals(parser.getName())){
51                     weatherInfos.add(weatherInfo);
52                     weatherInfo = null;
53                 }
54             default:
55                 break;
56             }
57             
58             
59             type = parser.next();
60         }
61         return weatherInfos;
62     }
63 }

4、weatherInfo类

 1 package com.example.myweather.domain;
 2 
 3 public class WeatherInfo {
 4 
 5     private int id;
 6     private String temp;
 7     private String weather;
 8     private String wind;
 9     private String name;
10     private String pm;
11     
12     
13     @Override
14     public String toString() {
15         return "WeatherInfo [id=" + id + ", temp=" + temp + ", weather="
16                 + weather + ", wind=" + wind + ", name=" + name + ", pm=" + pm
17                 + "]";
18     }
19     public int getId() {
20         return id;
21     }
22     public void setId(int id) {
23         this.id = id;
24     }
25     public String getTemp() {
26         return temp;
27     }
28     public void setTemp(String temp) {
29         this.temp = temp;
30     }
31     public String getWeather() {
32         return weather;
33     }
34     public void setWeather(String weather) {
35         this.weather = weather;
36     }
37     public String getWind() {
38         return wind;
39     }
40     public void setWind(String wind) {
41         this.wind = wind;
42     }
43     public String getName() {
44         return name;
45     }
46     public void setName(String name) {
47         this.name = name;
48     }
49     public String getPm() {
50         return pm;
51     }
52     public void setPm(String pm) {
53         this.pm = pm;
54     }
55 }

 

posted @ 2016-04-13 11:53  zhongyinghe  阅读(271)  评论(0编辑  收藏  举报