xml程序分析
来源:互联网 发布:seo是怎么优化的 编辑:程序博客网 时间:2024/06/03 23:41
请利用SAX编写程序解析Yahoo的XML格式的天气预报,获取当天和第二天的天气。
背景知识:handler是处理器。parser是解析器。parser负责读xml,handler负责处理parser读出的信息。
以下是别人的程序和自己的分析
#! -*- coding:utf-8 -*-from xml.parsers.expat import ParserCreateclass WeatherSaxHandler(object): def __init__(self): #定义了一个字典,装数据 self._data = {} #定义_dayCount来控制输出今天和明天的天气。这个要结合后面代码体会,很有意思。 self._dayCount = 0 @property def data(self): return self._data #分析xml,我们想要的信息都在start_element里。 def start_element(self, name, attrs): #如果name为'yweather:location',就把attrs['city']的值给self._data['city'] if name == 'yweather:location': self._data['city'] = attrs['city'] self._data['country'] = attrs['country'] elif name == 'yweather:forecast': if self._dayCount == 0: self._data['today'] = {} self._data['today']['text'] = attrs['text'] self._data['today']['low'] = int(attrs['low']) self._data['today']['high'] = int(attrs['high']) self._dayCount += 1 elif self._dayCount == 1: self._data['tomorrow'] = {} self._data['tomorrow']['text'] = attrs['text'] self._data['tomorrow']['low'] = int(attrs['low']) self._data['tomorrow']['high'] = int(attrs['high']) self._dayCount += 1 def end_element(self, name): pass def char_data(self, text): passdef parse_weather(xml): handler = WeatherSaxHandler() parser = ParserCreate() parser.StartElementHandler = handler.start_element parser.EndElementHandler = handler.end_element parser.CharacterDataHandler = handler.char_data parser.Parse(xml) #因为前面用了@property,所以这里用handler.data就可以。 return handler.datadata = r'''<?xml version="1.0" encoding="UTF-8" standalone="yes" ?><rss version="2.0" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"> <channel> <title>Yahoo! Weather - Beijing, CN</title> <lastBuildDate>Wed, 27 May 2015 11:00 am CST</lastBuildDate> <yweather:location city="Beijing" region="" country="China"/> <yweather:units temperature="C" distance="km" pressure="mb" speed="km/h"/> <yweather:wind chill="28" direction="180" speed="14.48" /> <yweather:atmosphere humidity="53" visibility="2.61" pressure="1006.1" rising="0" /> <yweather:astronomy sunrise="4:51 am" sunset="7:32 pm"/> <item> <geo:lat>39.91</geo:lat> <geo:long>116.39</geo:long> <pubDate>Wed, 27 May 2015 11:00 am CST</pubDate> <yweather:condition text="Haze" code="21" temp="28" date="Wed, 27 May 2015 11:00 am CST" /> <yweather:forecast day="Wed" date="27 May 2015" low="20" high="33" text="Partly Cloudy" code="30" /> <yweather:forecast day="Thu" date="28 May 2015" low="21" high="34" text="Sunny" code="32" /> <yweather:forecast day="Fri" date="29 May 2015" low="18" high="25" text="AM Showers" code="39" /> <yweather:forecast day="Sat" date="30 May 2015" low="18" high="32" text="Sunny" code="32" /> <yweather:forecast day="Sun" date="31 May 2015" low="20" high="37" text="Sunny" code="32" /> </item> </channel></rss>'''weather = parse_weather(data)assert weather['city'] == 'Beijing', weather['city']assert weather['country'] == 'China', weather['country']assert weather['today']['text'] == 'Partly Cloudy', weather['today']['text']assert weather['today']['low'] == 20, weather['today']['low']assert weather['today']['high'] == 33, weather['today']['high']assert weather['tomorrow']['text'] == 'Sunny', weather['tomorrow']['text']assert weather['tomorrow']['low'] == 21, weather['tomorrow']['low']assert weather['tomorrow']['high'] == 34, weather['tomorrow']['high']print('Weather:', str(weather))
阅读全文
0 0
- xml程序分析
- 微软XML分析程序介绍
- 构建轻量级XML DOM分析程序(1)
- 构建轻量级XML DOM分析程序(2)
- 构建轻量级XML DOM分析程序(3)
- 构建自己的轻量级XML DOM分析程序
- 程序分析
- 程序分析
- 程序分析
- 程序分析
- 【JavaWeb开发(二)】第一个jsp程序以及web.xml分析
- xml的分页程序
- 使用XPath分析XML
- ApplicationContext.xml的分析
- applicationContext.xml配置分析
- web.xml文件分析
- Tomcat Server XML分析
- XML解析分析
- FMI飞马网 |【线上直播】持续集成在京东研发的落地及应用
- Pycharm 中错误ImportError: No module named appium
- mysql 表锁住,mysql innodb Lock wait timeout exceeded; try restarting transaction
- javascript的正则表达式 积累
- 一阶惯性系统
- xml程序分析
- NEC单片机 0527系列编程解读
- nodejs内存溢出 FATAL ERROR: CALL_AND_RETRY_0 Allocation failed – process out of memory(转载)
- iOS-84-WKWebView字体大小颜色及内容放大或缩小显示
- eclipse 中 git 与 svn 共存
- java之foreach,你判null了吗?
- Spark Streaming获取kafka数据的两种方式
- 代理模式-----静态代理到动态代理的演化过程
- jQuery炫丽星空3d旋转星空