XML SAX解析

*****************************天气预报的实现****************************
代码:
public class WeatherReport extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.weather);
init();
}
private void init(){
Spinner city_spr = (Spinner)findViewById(R.id.Spinner01);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, ConstData.city);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
city_spr.setAdapter(adapter);
Button submit = (Button) findViewById(R.id.Button01);
submit.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Spinner spr = (Spinner)findViewById(R.id.Spinner01);
//获得下拉项的索引值
Long l = spr.getSelectedItemId();
int index = l.intValue();
//取出所选城市的坐标
String cityParamString = ConstData.cityCode[index];
try {
URL url = new URL(ConstData.queryString+cityParamString);
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
GoogleWeatherHandler gwh = new GoogleWeatherHandler();
xr.setContentHandler(gwh);
InputStreamReader isr =new InputStreamReader(url.openStream(),"GBK");
InputSource is=new InputSource(isr);
xr.parse(is);
WeatherSet ws = gwh.getMyWeatherSet();
updateWeatherInfoView(R.id.weather_0,ws.getMyCurrentCondition());
updateWeatherInfoView(R.id.weather_1,ws.getMyForecastConditions().get(0));
updateWeatherInfoView(R.id.weather_2,ws.getMyForecastConditions().get(1));
updateWeatherInfoView(R.id.weather_3,ws.getMyForecastConditions().get(2));
updateWeatherInfoView(R.id.weather_4,ws.getMyForecastConditions().get(3));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
private void updateWeatherInfoView(int aResourceID,WeatherCurrentCondition aWCC) throws MalformedURLException {
URL imgURL = new URL("http://www.google.com/" + aWCC.getIcon());
((SingleWeatherInfoView) findViewById(aResourceID)).setWeatherIcon(imgURL);
((SingleWeatherInfoView) findViewById(aResourceID)).setWeatherString(aWCC.toString());
}
private void updateWeatherInfoView(int aResourceID,WeatherForecastCondition aWFC) throws MalformedURLException {
URL imgURL = new URL("http://www.google.com/" + aWFC.getIcon());
((SingleWeatherInfoView) findViewById(aResourceID)).setWeatherIcon(imgURL);
((SingleWeatherInfoView) findViewById(aResourceID)).setWeatherString(aWFC.toString());
}
}

************************xml 的sax解析代码***************************

public class GoogleWeatherHandler extends DefaultHandler {

private WeatherSet myWeatherSet = null;

private boolean is_Current_Conditions = false;
private boolean is_Forecast_Conditions = false;
// private final String FORECAST_INFORMATION="forecast_information";
private final String CURRENT_CONDITIONS="current_conditions";
private final String FORECAST_CONDITIONS="forecast_conditions";

public GoogleWeatherHandler(){

}

public WeatherSet getMyWeatherSet() {
Log.i("igwh", " getMyWeatherSet");
return myWeatherSet;
}

@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
super.endDocument();
}

@Override
public void endElement(String uri, String localName, String name)
throws SAXException {

if(localName.equals(CURRENT_CONDITIONS)){
this.is_Current_Conditions = false;
}else if(localName.equals(FORECAST_CONDITIONS)){
this.is_Forecast_Conditions = false;
}
}

@Override
public void startDocument() throws SAXException {
this.myWeatherSet = new WeatherSet();
}

@Override
public void startElement(String uri, String localName, String name,
Attributes attributes) throws SAXException {
if(localName.equals(CURRENT_CONDITIONS)){
Log.i("localName+CURRENT", localName);
this.myWeatherSet.setMyCurrentCondition(new WeatherCurrentCondition());
Log.i("localName+CURRENT+1", localName);
this.is_Current_Conditions = true;
}else if(localName.equals(FORECAST_CONDITIONS)){
this.myWeatherSet.getMyForecastConditions().add(new WeatherForecastCondition());
this.is_Forecast_Conditions = true;
}else{
if(localName.equals(CURRENT_CONDITIONS)){
Log.i("localName+CURRENT", localName);
}
String dataAttribute = attributes.getValue("data");
//Shared Tags
if(localName.equals("icon")){
if(this.is_Current_Conditions){
this.myWeatherSet.getMyCurrentCondition().setIcon(dataAttribute);
}else if(this.is_Forecast_Conditions){
this.myWeatherSet.getLastForecastCondition().setIcon(dataAttribute);
}
}else if(localName.equals("condition")){
if(this.is_Current_Conditions){
this.myWeatherSet.getMyCurrentCondition().setCondition(dataAttribute);
}else if(this.is_Forecast_Conditions){
this.myWeatherSet.getLastForecastCondition().setCondition(dataAttribute);
}
}//Tags is current_conditions
else if(localName.equals("temp_c")){
this.myWeatherSet.getMyCurrentCondition().setTemp_celcius(dataAttribute);
}else if(localName.equals("humidity")){
this.myWeatherSet.getMyCurrentCondition().setHumidity(dataAttribute);
}else if(localName.equals("wind_condition")){
this.myWeatherSet.getMyCurrentCondition().setWind_condition(dataAttribute);
}//Tags is forecast_conditions
else if(localName.equals("day_of_week")){
this.myWeatherSet.getLastForecastCondition().setDay_of_week(dataAttribute);
}else if(localName.equals("low")){
this.myWeatherSet.getLastForecastCondition().setLow(dataAttribute);
}else if(localName.equals("high")){
this.myWeatherSet.getLastForecastCondition().setHigh(dataAttribute);
}
}

}
@Override
public void characters(char ch[], int start, int length) {
/*
* Would be called on the following structure: <element>characters</element>
*/
}

}

************************界面的xml代码实现******************************

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg">
<TableLayout
android:id="@+id/TableLayout02"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TableRow
android:id="@+id/TableRow01"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/msg"
android:textStyle="bold"
android:textSize="16px"
android:layout_marginLeft="10px"
android:textColor="@color/black">
</TextView>
<Spinner
android:id="@+id/Spinner01"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:paddingLeft="10px"
android:minWidth="200px">
</Spinner>
<Button
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/OK"
android:paddingLeft="10px">
</Button>
</TableRow>
</TableLayout>

<TableLayout
android:id="@+id/TableLayout01"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow
android:id="@+id/TableRow02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.dudo.weather.views.SingleWeatherInfoView
android:id="@+id/weather_0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow
android:id="@+id/TableRow03"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.dudo.weather.views.SingleWeatherInfoView
android:id="@+id/weather_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow
android:id="@+id/TableRow04"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.dudo.weather.views.SingleWeatherInfoView
android:id="@+id/weather_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow
android:id="@+id/TableRow05"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.dudo.weather.views.SingleWeatherInfoView
android:id="@+id/weather_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow
android:id="@+id/TableRow06"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.dudo.weather.views.SingleWeatherInfoView
android:id="@+id/weather_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</TableRow>
</TableLayout>
</LinearLayout>

**********************************************************
在界面的xml代码中有com.dudo.weather.views.SingleWeatherInfoView,该实现方式为代码实现,定义了当天与未来几天的天气显示布局,代码如下:

public class SingleWeatherInfoView extends LinearLayout{

private ImageView myWeatherImageView = null;
private TextView myTempTextView = null;

public SingleWeatherInfoView(Context context){
super(context);
}
public SingleWeatherInfoView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
this.myWeatherImageView = new ImageView(context);
this.myWeatherImageView.setPadding(10, 5, 5, 5);

this.myTempTextView = new TextView(context);
this.myTempTextView.setTextColor(Color.GREEN);
this.myTempTextView.setTextSize(16);

this.addView(this.myWeatherImageView, new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
this.addView(this.myTempTextView, new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}

public void setWeatherString(String aWeatherString) {
this.myTempTextView.setText(aWeatherString);
}
public void setWeatherIcon(URL aURL) {
try{
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
Bitmap bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
this.myWeatherImageView.setImageBitmap(bm);
}catch(Exception e){

}

}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值