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)

运行结果如下:

ElemenTree使用parse,formstring两种方式加载xml数据
tree = xml.etree.ElementTree.parse("text.xml")#文件
xml.etree.ElementTree.fromstring()#xml字符串
#根节点

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类型据此可做判断

可以通过以下方法判断某个节点是否还有子节点:
node.__len__() #0 表示无子节点
node.text #没有内容表示还有子节点
list(node)#空列表表示没有子节点
更多使用方法查看文档:xml.etree.ElementTree


0 0