要解析的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>
解析用到的JavaBean
package com.example.a19_xml_parse;
public class Channel {
public String temp;
public String city;
public String id;
public String pm250;
public String wind;
public String getTemp() {
return temp;
}
public void setTemp(String temp) {
this.temp = temp;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPm250() {
return pm250;
}
public void setPm250(String pm250) {
this.pm250 = pm250;
}
public String getWind() {
return wind;
}
public void setWind(String wind) {
this.wind = wind;
}
@Override
public String toString() {
return "Channel [id=" + id +", city=" + city + ", temp=" + temp + ", wind=" + wind + ", pm250=" +pm250 + "]";
}
}
解析者:
package com.example.a19_xml_parse;
import android.util.Xml;
import org.xmlpull.v1.XmlPullParser;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator on 2017/4/14.
*/
public class WeatherParser {
/**
* 服务器是以流的形式把数据返回的
* @return
*/
public static List<Channel> parserXml(InputStream in) throws Exception {
//0 声明集合对象
List<Channel> weatherLists = null;
Channel channel = null;
//1 获取XmlPullParser 解析的实例
XmlPullParser parser = Xml.newPullParser();
//2 设置XmlPullParser的参数
parser.setInput(in, "utf-8");
//3 获取事件类型
int type = parser.getEventType();
while (type != XmlPullParser.END_DOCUMENT) {
switch (type) {
case XmlPullParser.START_TAG: //解析开始标签
//4 具体判断一下 解析到哪个开始标志
if ("weather".equals(parser.getName())) {
//5 创建一个集合对象
weatherLists = new ArrayList<Channel>();
} else if ("channel".equals(parser.getName())) {
//6 创建channel对象
channel = new Channel();
//7 获取id值
String id = parser.getAttributeValue(0);
channel.setId(id);
} else if ("city".equals(parser.getName())) {
//8 获取city的数据
String city = parser.nextText();
channel.setCity(city);
} else if ("temp".equals(parser.getName())) {
String temp = parser.nextText();
channel.setTemp(temp);
} else if ("wind".equals(parser.getName())) {
String wind = parser.nextText();
channel.setWind(wind);
} else if ("pm250".equals(parser.getName())) {
String pm250 = parser.nextText();
channel.setPm250(pm250);
}
break;
case XmlPullParser.END_TAG: //解析结束标签
//判断要解析的结束标签
if ("channel".equals(parser.getName())) {
//把javabean对象存到集合中
weatherLists.add(channel);
}
break;
}
//不停的向下解析
type = parser.next();
}
return weatherLists;
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
//1 显示天气信息
TextView tv_weather = (TextView) findViewById(R.id.tv_weather);
//1.1 通过上下文获取资产的管理者
InputStream inputStream = getAssets().open("weather.xml");
//2 调用我们定义的解析xml业务方法
List<Channel> weatherLists = WeatherParser.parserXml(inputStream);
StringBuffer sb = new StringBuffer();
for (Channel channel : weatherLists) {
sb.append(channel.toString());
}
//3 把数据显示到textview上
tv_weather.setText(sb.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
StringBuffer线程安全,StringBuilder线程不安全