package com.lidaochen.phonecall; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Xml;
import android.view.View;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.ThemedSpinnerAdapter; import java.io.InputStream;
import java.util.ArrayList;
import java.util.List; public class MainActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try
{
// 找到TextView控件
TextView textView = (TextView)findViewById(R.id.textView);
// 通过上下文获取资产管理者
InputStream inputStream = getAssets().open("weather.xml");
// 调用我们定义的方法解析XML文件
List<Channel> weatherlists = WeatherParser.parserXml(inputStream);
StringBuffer sb = new StringBuffer();
for (Channel channel:weatherlists)
{
sb.append(channel.toString());
}
// 将解析的文件内容显示到TextView控件上面
textView.setText(sb.toString());
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
package com.lidaochen.phonecall; import android.util.Xml; import org.xmlpull.v1.XmlPullParser; import java.io.InputStream;
import java.util.ArrayList;
import java.util.List; public class WeatherParser {
// 服务器是以流的形式把数据返回的
public static List<Channel> parserXml(InputStream In) throws Exception
{
// 声明集合对象
List<Channel> weatherlists = null;
Channel channel = null;
// 获取XmlPullParser解析的实例
XmlPullParser xmlPullParser = Xml.newPullParser();
// 设置XmlPullParser的参数
xmlPullParser.setInput(In, "utf-8");
// 获取事件类型
int type = xmlPullParser.getEventType();
while(type != XmlPullParser.END_DOCUMENT)
{
switch (type)
{
case XmlPullParser.START_TAG:
if ("weather".equals(xmlPullParser.getName()))
{
// 创建一个集合对象
weatherlists = new ArrayList<Channel>();
}
else if ("channel".equals(xmlPullParser.getName()))
{
// 创建Channel对象
channel = new Channel();
// 获取id值
String id = xmlPullParser.getAttributeValue(0);
channel.setId(id);
}
else if ("city".equals(xmlPullParser.getName()))
{
String city = xmlPullParser.nextText();
channel.setCity(city);
}
else if ("temp".equals(xmlPullParser.getName()))
{
String temp = xmlPullParser.nextText();
channel.setTemp(temp);
}
else if ("wind".equals(xmlPullParser.getName()))
{
String wind = xmlPullParser.nextText();
channel.setWind(wind);
}
else if ("pm250".equals(xmlPullParser.getName()))
{
String pm250 = xmlPullParser.nextText();
channel.setPm250(pm250);
}
break;
case XmlPullParser.END_TAG: // 解析结束标志
// 判断要解析的结束标签是不是channel
if ("channel".equals(xmlPullParser.getName()))
{
// 把javabean对象存到集合中
weatherlists.add(channel);
}
break;
}
// 不停的向下解析
type = xmlPullParser.next();
} return weatherlists;
}
}
package com.lidaochen.phonecall; public class Channel {
private String id;
private String city;
private String temp;
private String wind;
private String pm250; public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getCity() {
return city;
} public void setCity(String city) {
this.city = city;
} public String getTemp() {
return temp;
} public void setTemp(String temp) {
this.temp = temp;
} public String getWind() {
return wind;
} public void setWind(String wind) {
this.wind = wind;
} public String getPm250() {
return pm250;
} public void setPm250(String pm250) {
this.pm250 = pm250;
} @Override
public String toString()
{
return "Channel [id=" + id + ", city=" + city + ", temp=" + temp
+ ", wind=" + wind + ", pm250=" + pm250 + "]\n";
}
}
下面是要解析的XML文件,需要放在assets目录下
<?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>