SAX處理XML例項

livedba發表於2011-04-19

本例項利用SAX(全稱是Simple API For XML)處理Google API返回的天氣預報資料,資料格式是XML,API地址是

下面是核心程式碼:

[@more@]

首先是天氣預報實體:

public class WeatherBean implements Serializable{

private static final long serialVersionUID = 1L;

//今天天氣
private String day;

private String lowTemp;

private String highTemp;

private String imageUrl;

private String condition;

因為SAX是基於事件的,必須定義一個Handler繼承DefaultHandler

public class WeatherReport extends DefaultHandler{

private boolean inForcast;

private WeatherBean currentWeather;

private List weatherList;

public WeatherReport(){
weatherList = new ArrayList();
this.inForcast=false;
}


public boolean isInForcast() {
return inForcast;
}


public void setInForcast(boolean inForcast) {
this.inForcast = inForcast;
}


public List getWeatherList() {
return weatherList;
}


public void setWeatherList(List weatherList) {
this.weatherList = weatherList;
}


@Override
public void startDocument(){

}

@Override
public void startElement(String uri,String localName,String qName,Attributes atts) throws SAXException{
Log.v("weather","開始解析XML");
String tagName = localName.length()!=0?localName:qName;
tagName = tagName.toLowerCase();
if(tagName.equals("forecast_conditions")){
inForcast = true;
currentWeather = new WeatherBean();
}

if(inForcast){
if(tagName.equals("day_of_week")){
currentWeather.setDay(atts.getValue("data"));
}else if(tagName.equals("low")){
currentWeather.setLowTemp(atts.getValue("data"));
}else if(tagName.equals("high")){
currentWeather.setHighTemp(atts.getValue("data"));
}else if(tagName.equals("icon")){
currentWeather.setImageUrl(atts.getValue("data"));
}else if(tagName.equals("condition")){
currentWeather.setCondition(atts.getValue("data"));
}
}
}

@Override
public void endElement(String uri,String localName,String qName) throws SAXException{
Log.v("weather","結束解析XML");
String tagName = localName.length()!=0?localName:qName;
tagName = tagName.toLowerCase();
if(tagName.equals("forecast_conditions")) {
inForcast = false;
weatherList.add(currentWeather);
}


}


}

然後是Activity的核心程式碼:

try{
WeatherReport wr = new WeatherReport();
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
XMLReader xml = parser.getXMLReader();
URL url = new URL("));
InputSource is = new InputSource(new InputStreamReader(url.openStream(),"GBK"));
xml.setContentHandler(wr);
xml.parse(is);

table.removeAllViews();
List list = wr.getWeatherList();
for(WeatherBean wb : list){
TableRow row = new TableRow(this);
row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
row.setGravity(Gravity.CENTER_VERTICAL);

TextView day = new TextView(this);
day.setText(wb.getDay());
day.setGravity(Gravity.CENTER_HORIZONTAL);

row.addView(day);

TextView temp = new TextView(this);
temp.setText(wb.getLowTemp()+"°C - "+wb.getHighTemp()+"°C");
temp.setGravity(Gravity.CENTER_HORIZONTAL);

row.addView(temp);

TextView condition = new TextView(this);
condition.setText(wb.getCondition());
condition.setGravity(Gravity.CENTER_HORIZONTAL);

row.addView(condition);
table.addView(row);
}

}catch(Exception e){
Log.d("weather","解析XML出錯了");
new AlertDialog.Builder(this)
.setTitle("解析XML錯誤")
.setMessage("獲取天氣資料失敗,請稍後再試")
.setNegativeButton("確定",null)
.show();
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/25261409/viewspace-1048842/,如需轉載,請註明出處,否則將追究法律責任。

相關文章