首页 / 操作系统 / Linux / Android 中使用Pull解析XML文件
解析XML文件是非常常用的功能,在Android客户端中,经常与服务器通信都需要xml文件的支持,我们这里介绍一个简单的xml文件的解析,就是使用android中的pull方法进行解析。在java中,有dom解析和sax解析,这个pull解析有些类似于sax解析,他也是一行一行的读取然后解析内容的方法。首先看一下这个简单的xml文件<?xml version="1.0" encoding="utf-8"?><infos> <city id="1">
<temp>-1℃/5℃</temp>
<weather>多云</weather>
<wind>南风3-4级</wind>
<name>上海</name>
<pm>200</pm>
</city><city id="2">
<temp>-1℃/5℃</temp>
<weather>多云</weather>
<wind>南风3-4级</wind>
<name>北京7-8</name>
<pm>800</pm>
</city>
<city id="3">
<temp>-7℃/5℃</temp>
<weather>多云</weather>
<wind>南风3-4级</wind>
<name>哈尔滨</name>
<pm>100</pm>
</city></infos>然后我们直接解析这个xml文件,在textview中显示一下这里是代码,首先是业务Beanpackage com.linuxidc.weather;public class WeatherBean { private int id;
private String name;
private String wind;
private String weather;
private String temp;
private String pm;
@Override
public String toString() {
return "WeatherBean [id=" + id + ", name=" + name + ", wind=" + wind
+ ", weather=" + weather + ", temp=" + temp + ", pm=" + pm
+ "]";
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} 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;
} public String getWeather() {
return weather;
} public void setWeather(String weather) {
this.weather = weather;
} public String getTemp() {
return temp;
} public void setTemp(String temp) {
this.temp = temp;
} public String getPm() {
return pm;
} public void setPm(String pm) {
this.pm = pm;
}
}然后是解析xml文件的主要代码package com.linuxidc.weather;import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;import org.xmlpull.v1.XmlPullParser;import android.util.Xml;public class ParseXml { public static List<WeatherBean> parse(InputStream is) {
List<WeatherBean> list = null;
WeatherBean bean = null;
try {
XmlPullParser parser = Xml.newPullParser();
// 初始化解析器
parser.setInput(is, "utf-8"); int type = parser.next();
while (type != XmlPullParser.END_DOCUMENT) {
switch (type) {
case XmlPullParser.START_TAG:
if ("infos".equals(parser.getName())) {
list = new ArrayList<WeatherBean>();
} else if ("city".equals(parser.getName())) {
bean = new WeatherBean();
bean.setId(Integer.valueOf(parser.getAttributeValue(0)));
} else if ("temp".equals(parser.getName())) {
String temp = parser.nextText();
bean.setTemp(temp);
} else if ("weather".equals(parser.getName())) {
String weather = parser.nextText();
bean.setWeather(weather);
} else if ("wind".equals(parser.getName())) {
String wind = parser.nextText();
bean.setWind(wind);
} else if ("name".equals(parser.getName())) {
String name = parser.nextText();
bean.setName(name);
} else if ("pm".equals(parser.getName())) {
String pm = parser.nextText();
bean.setPm(pm);
}
break; case XmlPullParser.END_TAG:
if ("city".equals(parser.getName())) {
// 一个城市的信息处理完毕
list.add(bean);
bean = null;
}
break;
} type = parser.next();
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
}最后在mainactivity中使用这个代码,使用类加载器完成这个简单的功能package com.linuxidc.weather;import java.util.List;import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;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);
List<WeatherBean> list = ParseXml.parse(MainActivity.class.getClassLoader().getResourceAsStream("test.xml"));
StringBuffer sb = new StringBuffer();
for(WeatherBean bean : list){
String str = bean.toString();
sb.append(str);
sb.append("
");
}
tv.setText(sb.toString());
}
}这样看来,解析xml文件还是非常简单的。更多Android相关信息见Android 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=11本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-09/106699.htm