使用SAX对XML进行解析,代码如下public class XmlHandler extends DefaultHandler {
private List<Weather> weatherList;
private boolean inForcast;
private Weather currentWeather;
public List<Weather> getWeatherList() {
return weatherList;
} public void setWeatherList(List<Weather> weatherList) {
this.weatherList = weatherList;
} public XmlHandler() {
weatherList = new ArrayList<Weather>();
inForcast = false;
}
public void startDocument() throws SAXException { // TODO Auto-generated method stub Log.i("------>", "文档开始"); super.startDocument(); } public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
Log.i("-------->", "一个元素解析开始 ");
String tagName = localName.length() != 0 ? localName : qName;
tagName = tagName.toLowerCase();
Log.i("-------->",localName);
if(tagName.equals("forecast_conditions")) {
Log.i("-------->", "一个元素解析开始1 ");
inForcast = true;
currentWeather = new Weather();
}
if(inForcast) {
Log.i("-------->", "一个元素解析开始2");
if(tagName.equals("day_of_week")) {
currentWeather.setDay(attributes.getValue("data"));
}else if(tagName.equals("low")) {
currentWeather.setLowTemp(attributes.getValue("data"));
}else if(tagName.equals("high")) {
currentWeather.setHighTemp(attributes.getValue("data"));
}else if(tagName.equals("icon")) {
currentWeather.setImageUrl(attributes.getValue("data"));
}else if(tagName.equals("condition")) {
currentWeather.setCondition(attributes.getValue("data"));
}
}
}
public void endDocument() throws SAXException { Log.i("------>", "文档结束"); super.endDocument(); }
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException { String tagName = localName.length() != 0 ? localName : qName;
tagName = tagName.toLowerCase();
if(tagName.equals("forecast_conditions")) {
inForcast = false;
weatherList.add(currentWeather);
Log.i("-------->", "一个元素解析完成");
}
}}
解析GOOGLE 天气api的时候正常,api地址 http://www.google.com/ig/api?hl=zh-cn&weather=beijing
然后我把api的页面直接保存下来,然后将文件传到自己的服务器空间里,http://wan.panxianhai.com/api.xml
再次解析结果发现SAX解析到一半就自己停止了,
log如下:03-16 07:10:53.522: I/------>(207): 文档开始
03-16 07:10:53.522: I/-------->(207): 一个元素解析开始
03-16 07:10:53.532: I/-------->(207): xml_api_reply
03-16 07:10:53.532: I/-------->(207): 一个元素解析开始
03-16 07:10:53.532: I/-------->(207): weather
03-16 07:10:53.532: I/-------->(207): 一个元素解析开始
03-16 07:10:53.532: I/-------->(207): forecast_information
03-16 07:10:53.532: I/-------->(207): 一个元素解析开始
03-16 07:10:53.532: I/-------->(207): city
03-16 07:10:53.532: I/-------->(207): 一个元素解析开始
03-16 07:10:53.532: I/-------->(207): postal_code
03-16 07:10:53.532: I/-------->(207): 一个元素解析开始
03-16 07:10:53.532: I/-------->(207): latitude_e6
03-16 07:10:53.532: I/-------->(207): 一个元素解析开始
03-16 07:10:53.532: I/-------->(207): longitude_e6
03-16 07:10:53.532: I/-------->(207): 一个元素解析开始
03-16 07:10:53.542: I/-------->(207): forecast_date
03-16 07:10:53.542: I/-------->(207): 一个元素解析开始
03-16 07:10:53.542: I/-------->(207): current_date_time
03-16 07:10:53.542: I/-------->(207): 一个元素解析开始
03-16 07:10:53.542: I/-------->(207): unit_system
03-16 07:10:53.542: I/-------->(207): 一个元素解析开始
03-16 07:10:53.542: I/-------->(207): current_conditions
03-16 07:10:53.542: I/-------->(207): 一个元素解析开始
03-16 07:10:53.542: I/-------->(207): condition
03-16 07:10:53.552: I/-------->(207): 一个元素解析开始
03-16 07:10:53.552: I/-------->(207): temp_f
03-16 07:10:53.552: I/-------->(207): 一个元素解析开始
03-16 07:10:53.552: I/-------->(207): temp_c
03-16 07:10:53.552: I/-------->(207): 一个元素解析开始
03-16 07:10:53.552: I/-------->(207): humidity
03-16 07:10:53.552: I/-------->(207): 一个元素解析开始
03-16 07:10:53.552: I/-------->(207): icon
03-16 07:10:53.802: D/ddm-heap(122): Got feature list request
03-16 07:10:53.812: D/ddm-heap(162): Got feature list request
03-16 07:10:53.852: D/Eas Debug(215): Logging:
03-16 07:10:53.902: D/EAS SyncManager(215): !!! EAS SyncManager, onDestroy
03-16 07:10:54.302: D/MediaScanner(162): opendir /system/media/ failed, errno: 2
03-16 07:10:54.312: D/MediaScanner(162): prescan time: 3872ms
03-16 07:10:54.312: D/MediaScanner(162): scan time: 11ms
03-16 07:10:54.312: D/MediaScanner(162): postscan time: 1ms
03-16 07:10:54.312: D/MediaScanner(162): total time: 3884ms
03-16 07:10:54.322: D/MediaScannerService(162): done scanning volume internal
03-16 07:10:54.322: D/MediaScannerService(162): start scanning volume external
03-16 07:10:54.332: V/MediaProvider(162): /sdcard volume ID: 233848072
03-16 07:10:54.392: V/MediaProvider(162): Attached volume: external
03-16 07:10:54.442: V/MediaScanner(162): pruneDeadThumbnailFiles... android.database.sqlite.SQLiteCursor@44c468e8
03-16 07:10:54.452: V/MediaScanner(162): /pruneDeadThumbnailFiles... android.database.sqlite.SQLiteCursor@44c468e8
03-16 07:10:54.452: D/MediaScanner(162): prescan time: 37ms
03-16 07:10:54.452: D/MediaScanner(162): scan time: 2ms
03-16 07:10:54.452: D/MediaScanner(162): postscan time: 15ms
03-16 07:10:54.452: D/MediaScanner(162): total time: 54ms
03-16 07:10:54.462: D/MediaScannerService(162): done scanning volume external求高手指点。
private List<Weather> weatherList;
private boolean inForcast;
private Weather currentWeather;
public List<Weather> getWeatherList() {
return weatherList;
} public void setWeatherList(List<Weather> weatherList) {
this.weatherList = weatherList;
} public XmlHandler() {
weatherList = new ArrayList<Weather>();
inForcast = false;
}
public void startDocument() throws SAXException { // TODO Auto-generated method stub Log.i("------>", "文档开始"); super.startDocument(); } public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
Log.i("-------->", "一个元素解析开始 ");
String tagName = localName.length() != 0 ? localName : qName;
tagName = tagName.toLowerCase();
Log.i("-------->",localName);
if(tagName.equals("forecast_conditions")) {
Log.i("-------->", "一个元素解析开始1 ");
inForcast = true;
currentWeather = new Weather();
}
if(inForcast) {
Log.i("-------->", "一个元素解析开始2");
if(tagName.equals("day_of_week")) {
currentWeather.setDay(attributes.getValue("data"));
}else if(tagName.equals("low")) {
currentWeather.setLowTemp(attributes.getValue("data"));
}else if(tagName.equals("high")) {
currentWeather.setHighTemp(attributes.getValue("data"));
}else if(tagName.equals("icon")) {
currentWeather.setImageUrl(attributes.getValue("data"));
}else if(tagName.equals("condition")) {
currentWeather.setCondition(attributes.getValue("data"));
}
}
}
public void endDocument() throws SAXException { Log.i("------>", "文档结束"); super.endDocument(); }
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException { String tagName = localName.length() != 0 ? localName : qName;
tagName = tagName.toLowerCase();
if(tagName.equals("forecast_conditions")) {
inForcast = false;
weatherList.add(currentWeather);
Log.i("-------->", "一个元素解析完成");
}
}}
解析GOOGLE 天气api的时候正常,api地址 http://www.google.com/ig/api?hl=zh-cn&weather=beijing
然后我把api的页面直接保存下来,然后将文件传到自己的服务器空间里,http://wan.panxianhai.com/api.xml
再次解析结果发现SAX解析到一半就自己停止了,
log如下:03-16 07:10:53.522: I/------>(207): 文档开始
03-16 07:10:53.522: I/-------->(207): 一个元素解析开始
03-16 07:10:53.532: I/-------->(207): xml_api_reply
03-16 07:10:53.532: I/-------->(207): 一个元素解析开始
03-16 07:10:53.532: I/-------->(207): weather
03-16 07:10:53.532: I/-------->(207): 一个元素解析开始
03-16 07:10:53.532: I/-------->(207): forecast_information
03-16 07:10:53.532: I/-------->(207): 一个元素解析开始
03-16 07:10:53.532: I/-------->(207): city
03-16 07:10:53.532: I/-------->(207): 一个元素解析开始
03-16 07:10:53.532: I/-------->(207): postal_code
03-16 07:10:53.532: I/-------->(207): 一个元素解析开始
03-16 07:10:53.532: I/-------->(207): latitude_e6
03-16 07:10:53.532: I/-------->(207): 一个元素解析开始
03-16 07:10:53.532: I/-------->(207): longitude_e6
03-16 07:10:53.532: I/-------->(207): 一个元素解析开始
03-16 07:10:53.542: I/-------->(207): forecast_date
03-16 07:10:53.542: I/-------->(207): 一个元素解析开始
03-16 07:10:53.542: I/-------->(207): current_date_time
03-16 07:10:53.542: I/-------->(207): 一个元素解析开始
03-16 07:10:53.542: I/-------->(207): unit_system
03-16 07:10:53.542: I/-------->(207): 一个元素解析开始
03-16 07:10:53.542: I/-------->(207): current_conditions
03-16 07:10:53.542: I/-------->(207): 一个元素解析开始
03-16 07:10:53.542: I/-------->(207): condition
03-16 07:10:53.552: I/-------->(207): 一个元素解析开始
03-16 07:10:53.552: I/-------->(207): temp_f
03-16 07:10:53.552: I/-------->(207): 一个元素解析开始
03-16 07:10:53.552: I/-------->(207): temp_c
03-16 07:10:53.552: I/-------->(207): 一个元素解析开始
03-16 07:10:53.552: I/-------->(207): humidity
03-16 07:10:53.552: I/-------->(207): 一个元素解析开始
03-16 07:10:53.552: I/-------->(207): icon
03-16 07:10:53.802: D/ddm-heap(122): Got feature list request
03-16 07:10:53.812: D/ddm-heap(162): Got feature list request
03-16 07:10:53.852: D/Eas Debug(215): Logging:
03-16 07:10:53.902: D/EAS SyncManager(215): !!! EAS SyncManager, onDestroy
03-16 07:10:54.302: D/MediaScanner(162): opendir /system/media/ failed, errno: 2
03-16 07:10:54.312: D/MediaScanner(162): prescan time: 3872ms
03-16 07:10:54.312: D/MediaScanner(162): scan time: 11ms
03-16 07:10:54.312: D/MediaScanner(162): postscan time: 1ms
03-16 07:10:54.312: D/MediaScanner(162): total time: 3884ms
03-16 07:10:54.322: D/MediaScannerService(162): done scanning volume internal
03-16 07:10:54.322: D/MediaScannerService(162): start scanning volume external
03-16 07:10:54.332: V/MediaProvider(162): /sdcard volume ID: 233848072
03-16 07:10:54.392: V/MediaProvider(162): Attached volume: external
03-16 07:10:54.442: V/MediaScanner(162): pruneDeadThumbnailFiles... android.database.sqlite.SQLiteCursor@44c468e8
03-16 07:10:54.452: V/MediaScanner(162): /pruneDeadThumbnailFiles... android.database.sqlite.SQLiteCursor@44c468e8
03-16 07:10:54.452: D/MediaScanner(162): prescan time: 37ms
03-16 07:10:54.452: D/MediaScanner(162): scan time: 2ms
03-16 07:10:54.452: D/MediaScanner(162): postscan time: 15ms
03-16 07:10:54.452: D/MediaScanner(162): total time: 54ms
03-16 07:10:54.462: D/MediaScannerService(162): done scanning volume external求高手指点。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货