Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> Android XML的操作(SAX)

Android XML的操作(SAX)

編輯:Android開發實例

在android平台下操作xml方式有很多種,常見的為SAX(Simple APIfor XML)和DOM(Document Object Model)。

 SAX操作xml的特點是當讀取xml文件的時候會隨時觸發事件,通過事件來處理當前讀取到的內容。這一點是跟dom有所不同的,dom是全部讀取完後在進行操作。

現在這個實例是以SAX進行XML操作的!

這個例子是讀取Google的天氣預報為例子做成了,使用的XML地址如下:

http://www.google.com/ig/api?weather=beijing&hl=zh-cn

 

通過互聯網獲取天氣的XML代碼,然後再通過SAX進行讀取:

在例子中只是讀取了當前的時時天氣,沒有對預報的內容進行讀取,等以後再完善吧:

首先根據XML文件抽象出一個類來,我獲取到的XML代碼如下:

 

代碼
<?xml version="1.0" ?>
<xml_api_reply version="1">
<weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1"
row="0" section="0">
<forecast_information>
<city data="Beijing, Beijing" />
<postal_code data="beijing" />
<latitude_e6 data="" />
<longitude_e6 data="" />
<forecast_date data="2010-12-27" />
<current_date_time data="2010-12-28 04:00:00 +0000" />
<unit_system data="SI" />
</forecast_information>
<current_conditions>
<condition data="晴" />
<temp_f data="28" />
<temp_c data="-2" />
<humidity data="濕度: 27%" />
<icon data="/ig/images/weather/sunny.gif" />
<wind_condition data="風向: 西北、風速:7 米/秒" />
</current_conditions>
<forecast_conditions>
<day_of_week data="周一" />
<low data="-12" />
<high data="6" />
<icon data="/ig/images/weather/sunny.gif" />
<condition data="晴" />
</forecast_conditions>
<forecast_conditions>
<day_of_week data="周二" />
<low data="-11" />
<high data="1" />
<icon data="/ig/images/weather/sunny.gif" />
<condition data="晴" />
</forecast_conditions>
<forecast_conditions>
<day_of_week data="周三" />
<low data="-11" />
<high data="2" />
<icon data="/ig/images/weather/chance_of_snow.gif" />
<condition data="可能降雪" />
</forecast_conditions>
<forecast_conditions>
<day_of_week data="周四" />
<low data="-13" />
<high data="-2" />
<icon data="/ig/images/weather/sunny.gif" />
<condition data="晴" />
</forecast_conditions>
</weather>
</xml_api_reply>

 

不同時間可能獲取到的不同,但是格式應該是一致的!

 

下面是根據這個抽象出來的類:

 

代碼
package com.SAXXMLReader;

public class NowWeather {
private String condition;
private String temp_f;
private String temp_c;
private String humidity;
private String icon;
private String wind_condition;

public NowWeather() {

}

public void setcondition(String condition) {
this.condition = condition;
}

public void settempf(String temp_f) {
this.temp_f = temp_f;
}

public void settempc(String temp_c) {
this.temp_c = temp_c;
}

public void sethumidity(String humidity) {
this.humidity = humidity;
}

public void seticon(String icon) {
this.icon = icon;
}

public void setwindcondition(String wind_condition) {
this.wind_condition = wind_condition;
}

public String getNowWeather()
{
StringBuilder strBuilder = new StringBuilder();
strBuilder.append(condition+"\n");
strBuilder.append(temp_f+"\n");
strBuilder.append(temp_c+"\n");
strBuilder.append(humidity+"\n");
strBuilder.append(icon+"\n");
strBuilder.append(wind_condition+"\n");

return strBuilder.toString();
}
}

 

這個類保存的是獲取到的數據,形式可能有多種,這個根據個人的習慣進行書寫吧。

 

寫到這裡,因為在SAX中使用的時候需要有一個DefaultHandler類的繼承,實現如下:

 

代碼
package com.SAXXMLReader;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

public class WeatherHandler extends DefaultHandler {

private final String CURRENT_CONDITIONS = "current_conditions"; // 當前
private final String forecast_conditions = "forecast_conditions"; // 當前
// 實時天氣信息
private boolean is_Current_Conditions = false;
// 預報天氣信息
private boolean is_Forecast_Conditions = false;


NowWeather nowWeather = new NowWeather();

@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
super.characters(ch, start, length);
}

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

public WeatherHandler() {

}

@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
// super.startElement(uri, localName, qName, attributes);
String dataAttribute = "OK";
// Log.d("WeatherHandler", localName);

if (localName.equals(CURRENT_CONDITIONS)) {
Log.d("WeatherHandler", localName);
is_Current_Conditions = true;
} else if (localName.equals(forecast_conditions)) {
is_Current_Conditions = false;
} else {
dataAttribute = attributes.getValue("data");
if (this.is_Current_Conditions) {

Log.d("WeatherHandler_1", dataAttribute);
// this.nowWeather.setcondition(dataAttribute);
if (localName.equals("condition")) {
this.nowWeather.setcondition(dataAttribute);
} else if (localName.equals("temp_f")) {
this.nowWeather.settempf(dataAttribute);
} else if (localName.equals("temp_c")) {
this.nowWeather.settempc(dataAttribute);
} else if (localName.equals("humidity")) {
this.nowWeather.sethumidity(dataAttribute);
} else if (localName.equals("icon")) {
this.nowWeather.seticon(dataAttribute);
} else if (localName.equals("wind_condition")) {
this.nowWeather.setwindcondition(dataAttribute);
}
} else if (this.is_Forecast_Conditions) {
Log.d("WeatherHandler_1", dataAttribute);
}

}
// Log.d("WeatherHandler_1", dataAttribute);
}

public String getNowWeather() {
return nowWeather.getNowWeather();
}

@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 {

}

}

 

 

在這裡實現了讀取XML代碼,並且保存到抽象出來的類中,以供調用。

 

下面的方法是對這個類的調用,通過調用,獲取內容:

 

 

代碼
    SAXParserFactory faction =SAXParserFactory.newInstance();
SAXParser parser = faction.newSAXParser();
WeatherHandler handler = new WeatherHandler();

XMLReader reader = parser.getXMLReader();

reader.setContentHandler(handler);

URL url = new URL(SRC);

HttpURLConnection httpconn = (HttpURLConnection) url.openConnection();
//httpconn.getInputStream();

InputStream inStream =httpconn.getInputStream();// this.getResources().openRawResource(R.xml.weather);
InputStreamReader isReader = new InputStreamReader(inStream,"GBK");
// BufferedReader buffRreader = new BufferedReader(isReader);
// String line="";
// String data = "";
//
// while((line=buffRreader.readLine())!=null)
// data += line;
// text1.setText(data);
//Toast.makeText(this,data, Toast.LENGTH_LONG).show();
InputSource inputSource = new InputSource(isReader);
reader.parse(inputSource);
text1.setText(handler.getNowWeather());
//Toast.makeText(this, handler.getNowWeather(), Toast.LENGTH_LONG).show();

 

 

這裡直接通過獲取網上的XML進行的解析,當然你也可以讀取本地的XML文件進行解析,這個是一樣的。因為有事情,這個寫的包括一些方法的命名可能不是怎麼規則,還請多多諒解。

如果代碼中有什麼錯誤,歡迎指正!

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved