python3 解析xml文件
来源:互联网 发布:c语言split函数 编辑:程序博客网 时间:2024/06/05 10:41
使用python3 xml.etree.ElementTree库解析xml文件
使用的xml文件内容如下:该数据来自百度天气api返回的xml格式的结果
<CityWeatherResponse>
<status>success</status>
<date>2014-04-27</date>
<results>
<currentCity>双牌</currentCity>
<weather_data>
<date>周六(实时:16℃)</date>
<dayPictureUrl>
http://api.map.baidu.com/images/weather/day/yin.png
</dayPictureUrl>
<nightPictureUrl>
http://api.map.baidu.com/images/weather/night/yin.png
</nightPictureUrl>
<weather>阴</weather>
<wind>北风微风</wind>
<temperature>15℃</temperature>
<date>周日</date>
<dayPictureUrl>
http://api.map.baidu.com/images/weather/day/duoyun.png
</dayPictureUrl>
<nightPictureUrl>
http://api.map.baidu.com/images/weather/night/duoyun.png
</nightPictureUrl>
<weather>多云</weather>
<wind>北风微风</wind>
<temperature>20 ~ 15℃</temperature>
<date>周一</date>
<dayPictureUrl>
http://api.map.baidu.com/images/weather/day/duoyun.png
</dayPictureUrl>
<nightPictureUrl>
http://api.map.baidu.com/images/weather/night/duoyun.png
</nightPictureUrl>
<weather>多云</weather>
<wind>北风微风</wind>
<temperature>22 ~ 15℃</temperature>
<date>周二</date>
<dayPictureUrl>
http://api.map.baidu.com/images/weather/day/zhenyu.png
</dayPictureUrl>
<nightPictureUrl>
http://api.map.baidu.com/images/weather/night/zhenyu.png
</nightPictureUrl>
<weather>阵雨</weather>
<wind>北风微风</wind>
<temperature>20 ~ 15℃</temperature>
</weather_data>
<currentCity>长沙</currentCity>
<weather_data>
<date>周六(实时:16℃)</date>
<dayPictureUrl>
http://api.map.baidu.com/images/weather/day/zhenyu.png
</dayPictureUrl>
<nightPictureUrl>
http://api.map.baidu.com/images/weather/night/zhenyu.png
</nightPictureUrl>
<weather>阵雨</weather>
<wind>微风</wind>
<temperature>15℃</temperature>
<date>周日</date>
<dayPictureUrl>
http://api.map.baidu.com/images/weather/day/duoyun.png
</dayPictureUrl>
<nightPictureUrl>
http://api.map.baidu.com/images/weather/night/duoyun.png
</nightPictureUrl>
<weather>多云</weather>
<wind>微风</wind>
<temperature>21 ~ 15℃</temperature>
<date>周一</date>
<dayPictureUrl>
http://api.map.baidu.com/images/weather/day/duoyun.png
</dayPictureUrl>
<nightPictureUrl>
http://api.map.baidu.com/images/weather/night/duoyun.png
</nightPictureUrl>
<weather>多云</weather>
<wind>微风</wind>
<temperature>24 ~ 15℃</temperature>
<date>周二</date>
<dayPictureUrl>
http://api.map.baidu.com/images/weather/day/duoyun.png
</dayPictureUrl>
<nightPictureUrl>
http://api.map.baidu.com/images/weather/night/duoyun.png
</nightPictureUrl>
<weather>多云</weather>
<wind>北风微风</wind>
<temperature>24 ~ 14℃</temperature>
</weather_data>
</results>
</CityWeatherResponse>
===========================>
def domParseXML(text=""):
if "" == text:
raise RuntimeError("解析的内容不能为空")
results = xml.etree.ElementTree.fromstring(text)
print("查询结果:",results.find("status").text,"查询日期:",results.find("date").text)
datas = results.find("results")
if datas is not None:
for data in datas:
if data.__len__() > 0:
for _,weather in enumerate(list(data)):
if weather.__len__() == 0:
text = weather.text.strip()
if text.startswith("http://"):
if text.find("day") > -1:
print("白天天气图标URL:",text)
else:
print("夜晚天气图片URL:",text)
else :
print(text)
else:
print("当前城市:",data.text)
运行结果如下:
root = tree.getroot()
root.tag,root.text,root.attrid获得节点的标签,文本内容,属性
#获得子节点 --->直系子节点
for child in root:
print(child.tag,child.text)
也可以通过find方式查找节点
root.find("results")------->返回类型是Element
root.findall("results")----->返回类型是list列表
如果未查找到则返回None类型据此可做判断
- python3 解析xml文件
- python3解析XML文件
- 使用python3.4解析xml文件(sax、dom、etree)
- python3 xml.dom.mini模块解析xml
- python3 xml.dom.pulldom 解析xml
- Python3.5环境下使用generateDS解析xml文件生成C++头文件的方法
- XML----SAX解析XML文件
- XML---DOM4J解析XML文件
- XML DOM解析XML文件
- XML SAX解析xml文件
- XML jDOM解析XML文件
- XML DOM4J解析XML文件
- 解析XML文件___SAX解析
- java 解析 xml文件
- XML文件解析
- XML文件解析
- JstL解析XML文件
- xml文件解析办法
- ORA-01034: ORACLE not available
- 树莓派ST7565P-12864液晶屏操作
- 如何用Java排序
- 计算器
- linux杂谈(七):磁盘阵列(RAID)
- python3 解析xml文件
- Java中反射机制和Class.forName、实例对象.class(属性)、实例对象getClass()的区别
- http://coolxing.iteye.com/blog/1279447
- 【前段开发】行内元素和块级元素总结(HTML CSS)
- 使用选项卡实现界面切换
- redis zmalloc
- 并查集
- ORA-01078: failure in processing system parameters
- 单链表