XML文件:
<?xml version="1.0" encoding="utf-8"?> <infos> <city id="1"> <name>上海</name> <temperature>15℃~20℃</temperature> <weather>多云转晴</weather> <wind>偏东风3~5级</wind> </city> <city id="1"> <name>北京</name> <temperature>18℃~22℃</temperature> <weather>阵雨转多云</weather> <wind>偏东风4~8级</wind> </city> <city id="1"> <name>哈尔滨</name> <temperature>5℃~10℃</temperature> <weather>晴</weather> <wind>偏东风1~2级</wind> </city> </infos>
Java代码:
package cn.jinyejun.android_domain; /** * 天气对象,用来保存天气所有属性 * * @author jinyejun * @date 2014-10-31 17:00 */ public class WeatherInfo { private int id; private String name; private String temperature; private String weather; private String wind; 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 getTemperature() { return temperature; } public void setTemperature(String temperature) { this.temperature = temperature; } public String getWeather() { return weather; } public void setWeather(String weather) { this.weather = weather; } public String getWind() { return wind; } public void setWind(String wind) { this.wind = wind; } @Override public String toString() { String str = String.format("%s\n温度:%s\n天气:%s\n风速:%s", name, temperature, weather, wind); System.out.println(); return str; } }
package cn.jinyejun.android_service; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import org.xmlpull.v1.XmlPullParser; import android.content.Context; import android.util.Xml; import android.widget.Toast; import cn.jinyejun.android_domain.WeatherInfo; /** * 天气业务类 用来解析xml文件获取天气信息 * * @author jinyejun date : 2014-10-31 17:00 */ public class WeatherService { /** * 用Pull方式解析xml文件 * * @param context * 上下文 * @param is * xml文件流 * @return weatherInfo 获取的天气集合 */ public final static List<WeatherInfo> getWeatherInfo(Context context, InputStream is) { //创建xml文件解析器 XmlPullParser parser = Xml.newPullParser(); try { //指定xml文件输入流以及编码方式 parser.setInput(is, "utf-8"); //创建天气对象用来存放天气各个属性 WeatherInfo info = null; //创建天气集合,用来保存解析到的每个天气对象 List<WeatherInfo> weatherInfo = null; //获取解析器解析到的事件类型 int type = parser.getEventType(); //当事件类型不是文档结束标志的时候继续执行 while (type != XmlPullParser.END_DOCUMENT) { switch (type) { //如果遇到开始标签,那么根据标签获取实际内容并且给天气对象对应属性赋值 case XmlPullParser.START_TAG: if ("infos".equals(parser.getName())) { weatherInfo = new ArrayList<WeatherInfo>(); } else if ("city".equals(parser.getName())) { info = new WeatherInfo(); String idStr = parser.getAttributeValue(null, "id"); info.setId(Integer.parseInt(idStr)); } else if ("name".equals(parser.getName())) { String name = parser.nextText(); info.setName(name); } else if ("temperature".equals(parser.getName())) { String temperature = parser.nextText(); info.setTemperature(temperature); } else if ("weather".equals(parser.getName())) { String weather = parser.nextText(); info.setWeather(weather); } else if ("wind".equals(parser.getName())) { String wind = parser.nextText(); info.setWind(wind); } break; //如果遇到结束标签说明已经解析完xml中一个节点 case XmlPullParser.END_TAG: if ("city".equals(parser.getName())) { //将获取到的天气对象放在天气集合中 weatherInfo.add(info); //释放info内存 info = null; } break; } //很重要!!必须移到下一个,不然就是死循环 type = parser.next(); } return weatherInfo; } catch (Exception e) { e.printStackTrace(); Toast.makeText(context, "读取文件失败", Toast.LENGTH_SHORT); return null; } } }
package cn.jinyejun.android_weatherreport; import java.io.InputStream; import java.util.List; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.widget.TextView; import cn.jinyejun.android_domain.WeatherInfo; import cn.jinyejun.android_service.WeatherService; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tv = (TextView) findViewById(R.id.tv_content); //用类加载器获取对应目录下的xml文件,放回文件输入流 InputStream is = MainActivity.class.getClassLoader().getResourceAsStream("weather.xml"); //获取天气集合 List<WeatherInfo> infos = WeatherService.getWeatherInfo(this, is); StringBuffer sb = new StringBuffer(); for(WeatherInfo info: infos){ sb.append(info.toString()); sb.append("\n\n"); } tv.setText(sb.toString()); } }