xml文件的pull解析

android应用程序中xml文件的pull解析:

要解析的xml文件为:info.xml,放置于src下

<?xml version="1.0" encoding="utf-8"?>
<info>
    <city name="北京">
	    <temp>28°C</temp>
		<wind>3~4级</wind>
		<sun>晴</sun>
	</city>
	<city name="天津">
		<temp>26°C</temp>
		<wind>3~4级</wind>
		<sun>雷阵雨</sun>
	</city>
	<city name="石家庄">
		<temp>46°C</temp>
		<wind>3~9级</wind>
		<sun>下雪</sun>
	</city>
	<city name="邯郸">
		<temp>16°C</temp>
		<wind>1~4级</wind>
		<sun>雷</sun>
	</city>
</info>
project文件结构如图:

将xml文件中的信息创建为JavaBean对象:

public class Weather {
   private String city;
   private String temp;
   private String wind;
   private String sun;
   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 getSun() {
		return sun;
	}
	public void setSun(String sun) {
		this.sun = sun;
	}
	@Override
	public String toString() {
		return "天气: " + city + ", 温度=" + temp + ", 风力=" + wind
				+","+  sun;
	}
}
WeatherService.java类文件用pull解析读取xml文件中的信息:

public class WeatherService {
    public static List<Weather> getWeatherInfo(InputStream is) throws Exception{
    	XmlPullParser parser=Xml.newPullParser();
    	parser.setInput(is, "utf-8");
    	//获取解析器解析的事件类型
    	int type=parser.getEventType();
    	List<Weather> weathers = null;
    	Weather weather = null;
    	while(type!=XmlPullParser.END_DOCUMENT){
    		switch(type){
    		//标签或者节点开始时候的节点
    		case XmlPullParser.START_TAG:
    			if("info".equals(parser.getName())){
    				//初始化天气集合信息
    				weathers=new ArrayList<Weather>();
    			}else if("city".equals(parser.getName())){
    				weather=new Weather();
    				String cityName=parser.getAttributeValue(0);
    				weather.setCity(cityName);
    			}else if("temp".equals(parser.getName())){
    				String temp=parser.nextText();
    				weather.setTemp(temp);
    			}else if("wind".equals(parser.getName())){
    				String wind=parser.nextText();
    				weather.setWind(wind);
    			}else if("sun".equals(parser.getName())){
    				String sun=parser.nextText();
    				weather.setSun(sun);
    			}
    			break;
    			//标签结束
    		case XmlPullParser.END_TAG:
    			//发现已解析完一个城市信息
    			if("city".equals(parser.getName())){
    				weathers.add(weather);
    				weather=null;
    			}
    			break;
    		}
    		//让解析器去解析下一个tag节点
    		type=parser.next();
    	}
		return weathers;
    }
}
在布局文件中设置一个TextViw用于显示从xml中读取的信息,MainActivity.java

public class MainActivity extends Activity {
     private TextView tv;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_main);
		tv=(TextView) findViewById(R.id.tv);
		InputStream in=getClassLoader().getResourceAsStream("info.xml");
		//解析xml获取天气信息
       try {
		 List<Weather> weathers= WeatherService.getWeatherInfo(in);
		 StringBuilder sb=new StringBuilder();
		 for(Weather weather:weathers){
			 sb.append(weather);
			 sb.append("\n");
		 }
		 tv.setText(sb.toString());
	    } catch (Exception e) {
		   e.printStackTrace();
		   Toast.makeText(this, "解析天气信息失败", 1).show();
	    }
	}
}

最终实现的效果如图:




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值