Android的基础学习:采用Pull方式解析XML文件(代码)

来源:互联网 发布:keep the windows open 编辑:程序博客网 时间:2024/04/29 08:30

采用Pull解析XML文件

1,在editplus中编写xml文件:

 

为了把xml映射到自己的app中,我们把写好的xml文件copy到当前应用的src目录下。

 

首先,照样,先从POLO开始写。weatherInfos.java文件,为了之后的输出方便,我们在这里重载一下tostring方法。


package com.example.weather.domain; public class WeatherInfos {private int id;private String name;private String wind;private String weather;private String temp;private String pm;@Overridepublic String toString() {return "天气预报: [城市id=" + id + ", "+"\n"+"名称=" + name + ",  "+"\n"+"风力=" + wind+ ",  "+"\n"+"天气状况=" + weather + ",  "+"\n"+"温度=" + temp + ",  "+"\n"+"pm2.5指数=" + 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;}} 


 

然后在写Service。

插一句:

xml解析有三种方式,DOM方式是加载内存,生成一个树状结构。它的特点是消耗内容比较大。SAX是基于事件的方式,它的特点是速度快,效率高,但是不能倒退。另一种就是Pull。

今天的练习我是按照Pull方式。它是将xml文件从上至下进行解析的。

 

package com.example.weather.service; import java.io.InputStream;import java.util.ArrayList;import java.util.List; import org.xmlpull.v1.XmlPullParser; import android.R.integer;import android.util.Xml; import com.example.weather.domain.WeatherInfos; public class WeatherService {public static List<WeatherInfos> getWeatherInfos(InputStream is) throws Exception{XmlPullParser parser=Xml.newPullParser();//解析器parser.setInput(is, "utf-8");List<WeatherInfos> weatherInfos=null;WeatherInfos 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<WeatherInfos>();}else if("city".equals(parser.getName())){weatherInfo=new WeatherInfos();String idString=parser.getAttributeValue(0);weatherInfo.setId(Integer.parseInt(idString));}else if("temp".equals(parser.getName())){String tempString=parser.nextText();weatherInfo.setTemp(tempString);}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;}}

写完Service之后,可以开始写mainactivity:

package com.example.weather; import java.util.List; import com.example.weather.domain.WeatherInfos;import com.example.weather.service.WeatherService; import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.widget.TextView;import android.widget.Toast; public class MainActivity extends Activity { @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);TextView tvTextView=(TextView) findViewById(R.id.weathertv);try {List<WeatherInfos> infos=WeatherService.getWeatherInfos(MainActivity.class.getClassLoader().getResourceAsStream("weather.xml"));//得到以这个命名文件的is StringBuffer sb=new StringBuffer();for(WeatherInfos info:infos){String str=info.toString();sb.append(str);sb.append("\n\n\n");}tvTextView.setText(sb);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();Toast.makeText(this, "解析失败", Toast.LENGTH_LONG).show();}}   } 


注释红色的部分:



最后运行,效果图如下:

 

 

采用断点调试的方式观察pull解析的流程:

将断点打到最容易出错的地方,比如说初始化解析器的地方。采用断点调试的方法,可以更清晰的了解pull解析xml文件的过程哦。

0 0