Android学习笔记——SAX解析XML

XML数据源:

<?xml version="1.0" encoding="utf-8"?>
<
WeatherWindow>
   <
updated>2015-09-08 20:00:00</updated>
   <
region><![CDATA[上海]]></region>
   <
today>
       <
condition><![CDATA[晴转多云]]></condition>
       <
temphigh>29</temphigh>
       <
templow>22</templow>
       <
image_url><![CDATA[http://media.lily.tvxio.com/icons/duoyun.png]]></image_url>
   </
today>
   <
tomorrow>
       <
condition><![CDATA[多云]]></condition>
       <
temphigh>29</temphigh>
       <
templow>29</templow>
       <
image_url><![CDATA[http://media.lily.tvxio.com/icons/duoyun.png]]></image_url>
   </
tomorrow>
</
WeatherWindow>

定义实体类:

package tv.ismar.daisy.data.weather;

/**
* Created by
<huaijiefeng@gmail.com> on 9/15/14.
*/
public class WeatherEntity {
   
private String updated;
   private
String region;

   private
WeatherDetail today;
   private
WeatherDetail tomorrow;


   public
String getUpdated() {
       
return updated;
   
}

   
public void setUpdated(String updated) {
       
this.updated = updated;
   
}

   
public String getRegion() {
       
return region;
   
}

   
public void setRegion(String region) {
       
this.region = region;
   
}

   
public WeatherDetail getToday() {
       
return today;
   
}

   
public void setToday(WeatherDetail today) {
       
this.today = today;
   
}

   
public WeatherDetail getTomorrow() {
       
return tomorrow;
   
}

   
public void setTomorrow(WeatherDetail tomorrow) {
       
this.tomorrow = tomorrow;
   
}

   
public static class WeatherDetail {

       
private String condition;
       private
String temphigh;
       private
String templow;
       private
String image_url;


       public
String getCondition() {
           
return condition;
       
}

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

       
public String getTemphigh() {
           
return temphigh;
       
}

       
public void setTemphigh(String temphigh) {
           
this.temphigh = temphigh;
       
}

       
public String getTemplow() {
           
return templow;
       
}

       
public void setTemplow(String templow) {
           
this.templow = templow;
       
}

       
public String getImage_url() {
           
return image_url;
       
}

       
public void setImage_url(String image_url) {
           
this.image_url = image_url;
       
}

   }
}

解析XML:

package tv.ismar.daisy.core.weather;

import
org.xml.sax.Attributes;
import
org.xml.sax.SAXException;
import
org.xml.sax.helpers.DefaultHandler;
import
tv.ismar.daisy.data.weather.WeatherEntity;

/**
* Created by huaijie on 9/15/15.
*/
public class WeatherInfoHandler extends DefaultHandler {

   
private final int TODAY = 5;
   private final int
TOMORROW = 6;

   private final int
UPDATED = 7;
   private final int
REGION = 8;


   private final int
CONDITION = 1;
   private final int
TEMPHIGH = 2;
   private final int
TEMPLOW = 3;
   private final int
IMAGE_URL = 4;

   private int
flg = 0;

   private
WeatherEntity weatherEntity;
   private
WeatherEntity.WeatherDetail weatherDetail;


   public
WeatherEntity getWeatherEntity() {
       
return weatherEntity;
   
}

   
@Override
   
public void startDocument() throws SAXException {
       
weatherEntity = new WeatherEntity();
   
}

   
@Override
   
public void endDocument() throws SAXException {
       
super.endDocument();
   
}


   
@Override
   
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
       
if (localName.equals("WeatherWindow")) {
           
flg = 0;
           return;
       
}

       
if (localName.equals("updated")) {
           
flg = UPDATED;
           return;
       
}

       
if (localName.equals("region")) {
           
flg = REGION;
           return;
       
}

       
if (localName.equals("today")) {
           
weatherDetail = new WeatherEntity.WeatherDetail();
           
flg = TODAY;
           return;
       
}


       
if (localName.equals("tomorrow")) {
           
weatherDetail = new WeatherEntity.WeatherDetail();
           
flg = TOMORROW;
           return;
       
}

       
if (localName.equals("condition")) {
           
flg = CONDITION;
           return;
       
}

       
if (localName.equals("temphigh")) {
           
flg = TEMPHIGH;
           return;
       
}

       
if (localName.equals("templow")) {
           
flg = TEMPLOW;
           return;
       
}

       
if (localName.equals("image_url")) {
           
flg = IMAGE_URL;
           return;
       
}


   }


   
@Override
   
public void endElement(String uri, String localName, String qName) throws SAXException {
       
if (localName.equals("today")) {
           
weatherEntity.setToday(weatherDetail);
           
weatherDetail = null;
           return;
       
}

       
if (localName.equals("tomorrow")) {
           
weatherEntity.setTomorrow(weatherDetail);
           
weatherDetail = null;
           return;
       
}
   }

   
@Override
   
public void characters(char[] ch, int start, int length) throws SAXException {
       String str =
new String(ch, start, length);

       switch
(flg) {
           
case UPDATED:
               
weatherEntity.setUpdated(str);
               break;
           case
REGION:
               
weatherEntity.setRegion(str);
               break;
           case
CONDITION:
               
weatherDetail.setCondition(str);
               break;
           case
TEMPHIGH:
               
weatherDetail.setTemphigh(str);
               break;
           case
TEMPLOW:
               
weatherDetail.setTemplow(str);
               break;
           case
IMAGE_URL:
               
weatherDetail.setImage_url(str);
               break;
           default
:
               
return;
       
}
   }
}


运行:

SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
try
{
   SAXParser saxParser = saxParserFactory.newSAXParser()
;
   
XMLReader xmlReader = saxParser.getXMLReader();
   
WeatherInfoHandler weatherInfoHandler = new WeatherInfoHandler();
   
xmlReader.setContentHandler(weatherInfoHandler);
   
InputSource inputSource = new InputSource(new StringReader(result));
   
xmlReader.parse(inputSource);

   
WeatherEntity weatherEntity = weatherInfoHandler.getWeatherEntity();

   
Log.i(TAG, "update: " + weatherEntity.getUpdated() + " region: " + weatherEntity.getRegion());
   
Log.i(TAG, "today condition: " + weatherEntity.getToday().getCondition());
   
Log.i(TAG, "today temphigh: " + weatherEntity.getToday().getTemphigh());
   
Log.i(TAG, "today templow: " + weatherEntity.getToday().getTemplow());
   
Log.i(TAG, "today image_url: " + weatherEntity.getToday().getImage_url());

   
Log.i(TAG, "tomorrow condition: " + weatherEntity.getTomorrow().getCondition());
   
Log.i(TAG, "tomorrow temphigh: " + weatherEntity.getTomorrow().getTemphigh());
   
Log.i(TAG, "tomorrow templow: " + weatherEntity.getTomorrow().getTemplow());
   
Log.i(TAG, "tomorrow image_url: " + weatherEntity.getTomorrow().getImage_url());


   
todayWeatherTemperature.setText(weatherEntity.getToday().getTemplow() + "℃ ~ " + weatherEntity.getToday().getTemphigh() + "℃");
   
todayWeatherInfo.setText(weatherEntity.getToday().getCondition());
   
Picasso.with(mContext).load(weatherEntity.getToday().getImage_url()).into(todayWeatherIcon1);


   
tomorrowWeatherTemperature.setText(weatherEntity.getTomorrow().getTemplow() + "℃ ~ " + weatherEntity.getTomorrow().getTemphigh() + "℃");
   
tomorrowWeatherInfo.setText(weatherEntity.getTomorrow().getCondition());
   
Picasso.with(mContext).load(weatherEntity.getToday().getImage_url()).into(tomorrowWeatherIcon1);


} catch (ParserConfigurationException e) {
   e.printStackTrace()
;
} catch (SAXException e) {
   e.printStackTrace()
;
} catch (IOException e) {
   e.printStackTrace()
;
}


你可能感兴趣的:(Android学习笔记——SAX解析XML)