学习Pull文件的解析方式:
1.得到一个解析器对象
XmlPullParser parser = Xml.newPullParser();
2.初始化解析器,第一个参数为输入流,第二对数为解析的编码
parser.setInput(is, "utf-8");
package com.example.weather;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import com.example.weather.domain.WeatherInfo;
import com.example.weather.service.WeatherService;
public class MainActivity extends Activity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv);
try {System.out.println("bug-->1");
//类加载为一个读取字节流,读取weather.xml文件
List<WeatherInfo> infos = WeatherService.getWeatherInfos(MainActivity.
class.getClassLoader().getResourceAsStream("weather.xml"));
StringBuffer sb = new StringBuffer();
for (WeatherInfo info: infos) {
String str = info.toString();
sb.append(str);
sb.append("\n");
}
tv.setText(sb.toString());
} catch (Exception e) {
Toast.makeText(this, "解析失败", 0).show();
e.printStackTrace();
}
}
}
package com.example.weather.service;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import android.util.Xml;
import com.example.weather.domain.WeatherInfo;
public class WeatherService {
public static List<WeatherInfo> getWeatherInfos(InputStream is) throws Exception{
XmlPullParser parser = Xml.newPullParser();
//初始化解析器,第一参数为一个读取流,第二为编码
parser.setInput(is, "utf-8");
List<WeatherInfo> weatherInfos = null;
WeatherInfo weatherInfo = null;
//得到当前事件类型返回一个整型0
int type = parser.getEventType();
//直到结束文档解析,也就是END_DOCUMENT循环结束
while (type != XmlPullParser.END_DOCUMENT) {
switch (type) {
//开始解析标签
case XmlPullParser.START_TAG:
//getName()为取得标签的名字,如果为"infos"说明为根标签,那么建立一个ArrayList<WeatherInfo>类型存放对象
if ("infos".equals(parser.getName())) {
weatherInfos = new ArrayList<WeatherInfo>();
//getName()取得标签名为"ciry",为对象标签,建立一个weatherInfo对象,看weather.xml就会明白
//getAttributeValue(0)取得id 属性
} else if ("city".equals(parser.getName())) {
weatherInfo = new WeatherInfo();
String idStr = parser.getAttributeValue(0);
weatherInfo.setId(Integer.parseInt(idStr));
} else if ("temp".equals(parser.getName())) {
//nextText()为指向下一个标签,也就是getName()的标签
String temp = parser.nextText();
weatherInfo.setTemp(temp);
} else if ("weather".equals(parser.getName())) {
String weather = parser.nextText();
weatherInfo.setWeather(weather);
} else if ("wind".equals(parser.getName())) {
String wind = parser.nextText();
weatherInfo.setWind(wind);
} else if ("name".equals(parser.getName())) {
String name = parser.nextText();
weatherInfo.setName(name);
} else if ("pm".equals(parser.getName())) {
String pm = parser.nextText();
weatherInfo.setPm(pm);
}
break;
//直到为结束标签,如果结束标签为"city",这时一个WeatherInfo对象解析完成,把对象加到ArrsyList<WeatherInfo>中
//将weatherInfo置为空,以便开始解析下一个WeatherInfo对象
case XmlPullParser.END_TAG:
if ("city".equals(parser.getName())) {
weatherInfos.add(weatherInfo);
weatherInfo = null;
}
break;
}
//结束循环的语句parser.next()指向下一个标签的作用
type = parser.next();
}
//返回List<WeatherInfo>对象
return weatherInfos;
}
}
package com.example.weather.domain;
public class WeatherInfo {
private int id;
private String wind;
private String weather;
private String name;
private String temp;
private String pm;
public WeatherInfo() {
}
public WeatherInfo(int id, String wind, String weather, String name,
String temp, String pm) {
super();
this.id = id;
this.wind = wind;
this.weather = weather;
this.name = name;
this.temp = temp;
this.pm = pm;
}
@Override
public String toString() {
return " [Cityid=" + id + ", 风力=" + wind + ", 天气情况="
+ weather + ", 名称=" + name + ", 温度=" + temp + ", pm2.5=" + pm
+ "]";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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 version="1.0" encoding="utf-8"?>
<infos>
<city id="1">
<temp>20*C/30*C</temp>
<weather>3月15日 多云转阴</weather>
<wind>南风1-5级</wind>
<name>上海</name>
<pm>200</pm>
</city>
<city id="2">
<temp>13*C/23*C</temp>
<weather>5月1日 晴</weather>
<wind>南风4-9级</wind>
<name>北京</name>
<pm>600</pm>
</city>
<city id="3">
<temp>14*C/29*C</temp>
<weather>7月24日 多云</weather>
<wind>北风7-9级</wind>
<name>武汉</name>
<pm>500</pm>
</city>
</infos>
全部代码:
package com.example.weather;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import com.example.weather.domain.WeatherInfo;
import com.example.weather.service.WeatherService;
public class MainActivity extends Activity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv);
try {System.out.println("bug-->1");
List<WeatherInfo> infos = WeatherService.getWeatherInfos(MainActivity.
class.getClassLoader().getResourceAsStream("weather.xml"));
StringBuffer sb = new StringBuffer();
for (WeatherInfo info: infos) {
String str = info.toString();
sb.append(str);
sb.append("\n");
}
tv.setText(sb.toString());
} catch (Exception e) {
Toast.makeText(this, "解析失败", 0).show();
e.printStackTrace();
}
}
}
package com.example.weather.service;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import android.util.Xml;
import com.example.weather.domain.WeatherInfo;
public class WeatherService {
public static List<WeatherInfo> getWeatherInfos(InputStream is) throws Exception{
XmlPullParser parser = Xml.newPullParser();
parser.setInput(is, "utf-8");
List<WeatherInfo> weatherInfos = null;
WeatherInfo weatherInfo = null;
int type = parser.getEventType();
while (type != XmlPullParser.END_DOCUMENT) {
switch (type) {
case XmlPullParser.START_TAG:
if ("infos".equals(parser.getName())) {
weatherInfos = new ArrayList<WeatherInfo>();
} else if ("city".equals(parser.getName())) {
weatherInfo = new WeatherInfo();
String idStr = parser.getAttributeValue(0);
weatherInfo.setId(Integer.parseInt(idStr));
} else if ("temp".equals(parser.getName())) {
String temp = parser.nextText();
weatherInfo.setTemp(temp);
} else if ("weather".equals(parser.getName())) {
String weather = parser.nextText();
weatherInfo.setWeather(weather);
} else if ("wind".equals(parser.getName())) {
String wind = parser.nextText();
weatherInfo.setWind(wind);
} else if ("name".equals(parser.getName())) {
String name = parser.nextText();
weatherInfo.setName(name);
} else if ("pm".equals(parser.getName())) {
String pm = parser.nextText();
weatherInfo.setPm(pm);
}
break;
case XmlPullParser.END_TAG:
if ("city".equals(parser.getName())) {
weatherInfos.add(weatherInfo);
weatherInfo = null;
}
break;
}
type = parser.next();
}
return weatherInfos;
}
}
package com.example.weather.domain;
public class WeatherInfo {
private int id;
private String wind;
private String weather;
private String name;
private String temp;
private String pm;
public WeatherInfo() {
}
public WeatherInfo(int id, String wind, String weather, String name,
String temp, String pm) {
super();
this.id = id;
this.wind = wind;
this.weather = weather;
this.name = name;
this.temp = temp;
this.pm = pm;
}
@Override
public String toString() {
return " [Cityid=" + id + ", 风力=" + wind + ", 天气情况="
+ weather + ", 名称=" + name + ", 温度=" + temp + ", pm2.5=" + pm
+ "]";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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 version="1.0" encoding="utf-8"?>
<infos>
<city id="1">
<temp>20*C/30*C</temp>
<weather>3月15日 多云转阴</weather>
<wind>南风1-5级</wind>
<name>上海</name>
<pm>200</pm>
</city>
<city id="2">
<temp>13*C/23*C</temp>
<weather>5月1日 晴</weather>
<wind>南风4-9级</wind>
<name>北京</name>
<pm>600</pm>
</city>
<city id="3">
<temp>14*C/29*C</temp>
<weather>7月24日 多云</weather>
<wind>北风7-9级</wind>
<name>武汉</name>
<pm>500</pm>
</city>
</infos>