android 用pull解析xml文件

一:xml文件:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<infos>
 <city id="1">
 <temperature>20-30度</temperature>
 <wind>南风3-4级</wind>
 <weather>5月20日 多云转阴</weather>
 <name>上海</name>
 <pm>200</pm>
 </city>
 <city id="2">
 <temperature>26-30度</temperature>
 <wind>南风7-8级</wind>
 <weather>5月20日 多云转阴</weather>
 <name>北京</name>
 <pm>800</pm>
 </city>
 <city id="3">
 <temperature>28-33度</temperature>
 <wind>南风5-6级</wind>
 <weather>5月20日 多云转阴</weather>
 <name>四川</name>
 <pm>400</pm>
 </city>
</infos>



二:WeatherInfo类
    1,里面包含一个city的属性
    2,toString方法
    3,代码
 public class WeatherInfo {
 private int id;
 private String temperature;
 private String wind;
 private String weather;
 private String pm;
 private String name;
 
 @Override
 public String toString() {
  return "[城市id=" + id + ", 天气=" + temperature
    + ", 风力=" + wind + ", 天气状况=" + weather + ", pm2.5=" + pm
    + ", 城市名=" + name + "]";
 }
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public String getTemperature() {
  return temperature;
 }
 public void setTemperature(String temperature) {
  this.temperature = temperature;
 }
 public String getWind() {
  return wind;
 }
 public void setWind(String wind) {
  this.wind = wind;
 }
 public String getWeather() {
  return weather;
 }
 public void setWeather(String weather) {
  this.weather = weather;
 }
 public String getPm() {
  return pm;
 }
 public void setPm(String pm) {
  this.pm = pm;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
}


三:解析xml文件步骤:
public class WeahterService {
 public static List<WeatherInfo> getWeatherInfo(InputStream is) throws Exception {
  // 创建xml解析器pull
  XmlPullParser parser = Xml.newPullParser();
  // 初始化解析器
  parser.setInput(is, "utf-8");
  List<WeatherInfo> list = null;
  WeatherInfo wi = null;
  // 获取当前事件的类型
  int type = parser.getEventType();
  // 当解析没有到结束头部的时候,一直循环
  while (type != XmlPullParser.END_DOCUMENT) {
   switch (type) {
   // 解析到开始标签
case XmlPullParser.START_DOCUMENT:
list = new ArrayList<WeatherInfo>();
break;
   case XmlPullParser.START_TAG:
    if ("city".equals(parser.getName())) {
     wi = new WeatherInfo();
     String strId = parser.getAttributeValue(0); // 获取属性值 id
     wi.setId(Integer.parseInt(strId));
    } else if ("temperature".equals(parser.getName())) {
     // 获取温度信息
     String temp = parser.nextText();
     wi.setTemperature(temp);
    } else if ("wind".equals(parser.getName())) {
     String wind = parser.nextText();
     wi.setWind(wind);
    } else if ("weather".equals(parser.getName())) {
     String weather = parser.nextText();
     wi.setWeather(weather);
    } else if ("name".equals(parser.getName())) {
     String name = parser.nextText();
     wi.setName(name);
    } else if ("pm".equals(parser.getName())) {
     String pm = parser.nextText();
     wi.setPm(pm);
    }
    break;
   case XmlPullParser.END_TAG:
    if ("city".equals(parser.getName())) {
     list.add(wi);
     wi = null;
    }
    break;
   }
   type = parser.next();
  }
  return list;
 }
}




返回一个 List<WeatherInfo> 集合

四:activity中
public class MainActivity extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  TextView tv = (TextView) findViewById(R.id.tv_out);
 
  try {
   // 加载xml文件,得到一个List<WeatherInfo>集合
   List<WeatherInfo> list = WeahterService.getWeatherInfo(MainActivity.class.getClassLoader()
     .getResourceAsStream("weather.xml"));
   StringBuffer sb = new StringBuffer();
   for (WeatherInfo info : list) {
    String str = info.toString();
    sb.append(str);
    sb.append("\n\n");
   }
   tv.setText(sb.toString());
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   Toast.makeText(this, "解析失败", Toast.LENGTH_LONG).show();
  }
 }
}



以上代码复制之后可是直接运行,但是建议自己慢慢写。

1:定义类封装类WeatherInfo
2:定义解析xml的方法,返回一个List<WeatherInfo>集合的方法
3:主线程获取List<WeatherInfo>,并在布局用textview中显示

你可能感兴趣的:(android,xml,技术,tag)