首页 > 代码库 > 序列化xml文件实例——天气预报
序列化xml文件实例——天气预报
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivityForecast" >
<Button
android:id="@+id/btn_xlh"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btn_watch"
android:layout_alignParentTop="true"
android:layout_marginTop="60dp"
android:text="序列化xml文件" />
<Button
android:id="@+id/btn_watch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_xlh"
android:layout_centerVertical="true"
android:text="安阳天气如何"/>
</RelativeLayout>
<------------------------------------------------------------------------------------------------------------------>
<?xml version="1.0" encoding="utf-8"?>
<city>
<name>安阳</name>
<weather>晴天</weather>
<temp>27℃</temp>
<info>天气凉爽适宜出行</info>
</city>
<------------------------------------------------------------------------------------------------------------------->
package cn.itcast.weatherForecast;
import java.io.InputStream;
import org.xmlpull.v1.XmlPullParser;
import cn.itcast.weatherForecast.bean.WeatherBean;
import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Xml;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivityForecast extends Activity {
private Button btn_xlh;
private Button btn_watch;
private boolean flag=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity_forecast);
btn_xlh=(Button)findViewById(R.id.btn_xlh);
btn_xlh=(Button)findViewById(R.id.btn_watch);
btn_xlh.setOnClickListener(new MyOnClickListener());
btn_watch.setOnClickListener(new MyOnClickListener());
}
private class MyOnClickListener implements OnClickListener{
private WeatherBean wb = new WeatherBean();
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.btn_xlh:
flag=true;
AssetManager am = MainActivityForecast.this.getAssets();
try {
InputStream is = am.open("weatherForecast.xml");
XmlPullParser parser = Xml.newPullParser();
parser.setInput(is, "utf-8");
int type = parser.getEventType();
while(type!=XmlPullParser.END_DOCUMENT){
if(type==XmlPullParser.START_TAG){
if(parser.getName().equals("name")){
wb.setName(parser.nextText());
}else if(parser.getName().equals("weather")){
wb.setWeather(parser.nextText());
}else if(parser.getName().equals("temp")){
wb.setTemp(parser.nextText());
}else if(parser.getName().equals("info")){
wb.setInfo(parser.nextText());
}
}
type=parser.next();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case R.id.btn_watch:
if(flag){
Toast.makeText(MainActivityForecast.this, wb.toString(), 1).show();
}else{
Toast.makeText(MainActivityForecast.this, "未进行序列化,无法查看天气", 1).show();
}
break;
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_activity_forecast, menu);
return true;
}
}
<------------------------------------------------------------------------------------------------------------------->
package cn.itcast.weatherForecast.bean;
public class WeatherBean {
private String name;
private String weather;
private String temp;
private String info;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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 getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
@Override
public String toString() {
return "天气情况: [name=" + name + ", weather=" + weather + ", temp="
+ temp + ", info=" + info + "]";
}
}
<------------------------------------------------------------------------------------------------------------------->
<------------------------------------------------------------------------------------------------------------------->
序列化xml文件实例——天气预报