Android手机开发专题博客

Android手机开发专题博客,为您精选安卓手机开发教程,助您手机开发愉快!

公告信息
欢迎光临Android手机开发专题博客,祝您手机开发愉快!
文章档案

Android使用SAX解析XML与读写操作示例

在手机操作里,为了节省空间,多数采用xml做为存储介质,因此,如何用Android解析XML与使用Android读写XML操作,变的相当重要,android操作xml同时也是基础操作之一,对于Android手机开者而言,这是一项必备的基础知识。同时,android平台为此也提供了很多API用于操作xml,如Simple APIfor XML,简称(SAX)、Document Object Model,简称DOM等方式。

本节为您介绍使用SAX来解析与进行Android的XML读写操作

SAX操作xml

SAX在读取xml文件时会触发事件,通过事件来处理当前读取到的内容。

这一点区别于dom,dom是全部读取完后在进行操作。

下面来个示例:读取google天气预报:

读取地址:http://www.google.com/ig/api?weather=guangzhou&hl=zh-cn

通过网址URL取得天气的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="guangzhou, guangzhou" />
<postal_code data="guangzhou" />
<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();
}
}

此类根据习惯编写而成,用于保存GET到的数据,当然形式多种多样,大伙根据自己的习惯即可。

由于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 {

super.characters(ch, start, length);
}

@Override
public void startDocument() throws SAXException {

super.startDocument();
}

public WeatherHandler() {

}

@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes)
throws SAXException {

String dataAttribute = "OK";


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);

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);
}

}

}

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

@Override
public void endDocument() throws SAXException {

super.endDocument();
}

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

}

}

 以上代码用于实现Android读取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();


InputStream inStream
=httpconn.getInputStream();
InputStreamReader isReader = new InputStreamReader(inStream,"GBK");

InputSource inputSource = new InputSource(isReader);
reader.parse(inputSource);
text1.setText(handler.getNowWeather());

以上的示例中,直接用网址URL进行读取,当然读取本机的XML也是一样的道理,大伙应该可以理解,本节示例到此结束,希望对新手有所帮助。


新浪微博粉丝精灵,刷粉丝、刷评论、刷转发、企业商家微博营销必备工具"

2011/9/5 9:40:05 | android开发教程 | |

  • 发表评论