python解析xml简单示例


Posted in Python onJune 21, 2019

本文实例讲述了python解析xml的方法。分享给大家供大家参考,具体如下:

xml是除了json之外另外一个比较常用的用来做为数据交换的载体格式。对于一些比较固定的数据,直接保存在xml中,还可以免去去数据库中查询的麻烦。而且直接读小文件,性能比查询数据库应该更好,下面一个例子,如何用python解析xml数据,xml数据是省份,城市 数据,内容如下:

<?xml version="1.0" encoding="utf-8" ?>
<country>
  <province name="直辖市">
    <city name="北京"></city>
    <city name="上海"></city>
    <city name="天津"></city>
    <city name="重庆"></city>
  </province>
  <province name="黑龙江省">
    <city name="哈尔滨"></city>
    <city name="齐齐哈尔"></city>
    <city name="鸡西"></city>
    <city name="鹤岗"></city>
    <city name="双鸭山"></city>
    <city name="大庆"></city>
    <city name="伊春"></city>
    <city name="佳木斯"></city>
    <city name="七台河"></city>
    <city name="牡丹江"></city>
    <city name="黑河"></city>
    <city name="绥化"></city>
    <city name="大兴安岭地区"></city>
  </province>
  <province name="吉林省">
    <city name="长春"></city>
    <city name="吉林"></city>
    <city name="四平"></city>
    <city name="辽源"></city>
    <city name="通化"></city>
    <city name="白山"></city>
    <city name="松原"></city>
    <city name="白城"></city>
    <city name="延边朝鲜族自治州"></city>
  </province>
  <province name="辽宁省">
    <city name="沈阳"></city>
    <city name="大连"></city>
    <city name="鞍山"></city>
    <city name="抚顺"></city>
    <city name="本溪"></city>
    <city name="丹东"></city>
    <city name="锦州"></city>
    <city name="营口"></city>
    <city name="阜新"></city>
    <city name="辽阳"></city>
    <city name="盘锦"></city>
    <city name="铁岭"></city>
    <city name="朝阳"></city>
    <city name="葫芦岛"></city>
  </province>
  <province name="山东省">
    <city name="济南"></city>
    <city name="青岛"></city>
    <city name="淄博"></city>
    <city name="枣庄"></city>
    <city name="东营"></city>
    <city name="烟台"></city>
    <city name="潍坊"></city>
    <city name="济宁"></city>
    <city name="泰安"></city>
    <city name="威海"></city>
    <city name="日照"></city>
    <city name="莱芜"></city>
    <city name="临沂"></city>
    <city name="德州"></city>
    <city name="聊城"></city>
    <city name="滨州"></city>
    <city name="菏泽"></city>
  </province>
  <province name="山西省">
    <city name="太原"></city>
    <city name="大同"></city>
    <city name="阳泉"></city>
    <city name="长治"></city>
    <city name="晋城"></city>
    <city name="朔州"></city>
    <city name="晋中"></city>
    <city name="运城"></city>
    <city name="忻州"></city>
    <city name="临汾"></city>
    <city name="吕梁"></city>
  </province>
  <province name="陕西省">
    <city name="西安"></city>
    <city name="铜川"></city>
    <city name="宝鸡"></city>
    <city name="咸阳"></city>
    <city name="渭南"></city>
    <city name="延安"></city>
    <city name="汉中"></city>
    <city name="榆林"></city>
    <city name="安康"></city>
    <city name="商洛"></city>
  </province>
  <province name="河北省">
    <city name="石家庄"></city>
    <city name="唐山"></city>
    <city name="秦皇岛"></city>
    <city name="邯郸"></city>
    <city name="邢台"></city>
    <city name="保定"></city>
    <city name="张家口"></city>
    <city name="承德"></city>
    <city name="沧州"></city>
    <city name="廊坊"></city>
    <city name="衡水"></city>
  </province>
  <province name="河南省">
    <city name="郑州"></city>
    <city name="开封"></city>
    <city name="洛阳"></city>
    <city name="平顶山"></city>
    <city name="安阳"></city>
    <city name="鹤壁"></city>
    <city name="新乡"></city>
    <city name="焦作"></city>
    <city name="济源"></city>
    <city name="濮阳"></city>
    <city name="许昌"></city>
    <city name="漯河"></city>
    <city name="三门峡"></city>
    <city name="南阳"></city>
    <city name="商丘"></city>
    <city name="信阳"></city>
    <city name="周口"></city>
    <city name="驻马店"></city>
  </province>
  <province name="湖北省">
    <city name="武汉"></city>
    <city name="黄石"></city>
    <city name="十堰"></city>
    <city name="宜昌"></city>
    <city name="襄樊"></city>
    <city name="鄂州"></city>
    <city name="荆门"></city>
    <city name="孝感"></city>
    <city name="荆州"></city>
    <city name="黄冈"></city>
    <city name="咸宁"></city>
    <city name="随州"></city>
    <city name="恩施土家族苗族自治州"></city>
    <city name="仙桃"></city>
    <city name="潜江"></city>
    <city name="天门"></city>
    <city name="神农架林区"></city>
  </province>
  <province name="湖南省">
    <city name="长沙"></city>
    <city name="株洲"></city>
    <city name="湘潭"></city>
    <city name="衡阳"></city>
    <city name="邵阳"></city>
    <city name="岳阳"></city>
    <city name="常德"></city>
    <city name="张家界"></city>
    <city name="益阳"></city>
    <city name="郴州"></city>
    <city name="永州"></city>
    <city name="怀化"></city>
    <city name="娄底"></city>
    <city name="湘西土家族苗族自治州"></city>
  </province>
  <province name="海南省">
    <city name="海口"></city>
    <city name="三亚"></city>
    <city name="五指山"></city>
    <city name="琼海"></city>
    <city name="儋州"></city>
    <city name="文昌"></city>
    <city name="万宁"></city>
    <city name="东方"></city>
    <city name="定安县"></city>
    <city name="屯昌县"></city>
    <city name="澄迈县"></city>
    <city name="临高县"></city>
    <city name="白沙黎族自治县"></city>
    <city name="昌江黎族自治县"></city>
    <city name="乐东黎族自治县"></city>
    <city name="陵水黎族自治县"></city>
    <city name="保亭黎族苗族自治县"></city>
    <city name="琼中黎族苗族自治县"></city>
  </province>
  <province name="江苏省">
    <city name="南京"></city>
    <city name="无锡"></city>
    <city name="徐州"></city>
    <city name="常州"></city>
    <city name="苏州"></city>
    <city name="南通"></city>
    <city name="连云港"></city>
    <city name="淮安"></city>
    <city name="盐城"></city>
    <city name="扬州"></city>
    <city name="镇江"></city>
    <city name="泰州"></city>
    <city name="宿迁"></city>
  </province>
  <province name="江西省">
    <city name="南昌"></city>
    <city name="景德镇"></city>
    <city name="萍乡"></city>
    <city name="九江"></city>
    <city name="新余"></city>
    <city name="鹰潭"></city>
    <city name="赣州"></city>
    <city name="吉安"></city>
    <city name="宜春"></city>
    <city name="抚州"></city>
    <city name="上饶"></city>
  </province>
  <province name="广东省">
    <city name="广州"></city>
    <city name="韶关"></city>
    <city name="深圳"></city>
    <city name="珠海"></city>
    <city name="汕头"></city>
    <city name="佛山"></city>
    <city name="江门"></city>
    <city name="湛江"></city>
    <city name="茂名"></city>
    <city name="肇庆"></city>
    <city name="惠州"></city>
    <city name="梅州"></city>
    <city name="汕尾"></city>
    <city name="河源"></city>
    <city name="阳江"></city>
    <city name="清远"></city>
    <city name="东莞"></city>
    <city name="中山"></city>
    <city name="潮州"></city>
    <city name="揭阳"></city>
    <city name="云浮"></city>
  </province>
  <province name="广西省">
    <city name="南宁"></city>
    <city name="柳州"></city>
    <city name="桂林"></city>
    <city name="梧州"></city>
    <city name="北海"></city>
    <city name="防城港"></city>
    <city name="钦州"></city>
    <city name="贵港"></city>
    <city name="玉林"></city>
    <city name="百色"></city>
    <city name="贺州"></city>
    <city name="河池"></city>
    <city name="来宾"></city>
    <city name="崇左"></city>
  </province>
  <province name="云南省">
    <city name="昆明"></city>
    <city name="曲靖"></city>
    <city name="玉溪"></city>
    <city name="保山"></city>
    <city name="昭通"></city>
    <city name="丽江"></city>
    <city name="思茅"></city>
    <city name="临沧"></city>
    <city name="楚雄彝族自治州"></city>
    <city name="红河哈尼族彝族自治州"></city>
    <city name="文山壮族苗族自治州"></city>
    <city name="西双版纳傣族自治州"></city>
    <city name="大理白族自治州"></city>
    <city name="德宏傣族景颇族自治州"></city>
    <city name="怒江傈僳族自治州"></city>
    <city name="迪庆藏族自治州"></city>
  </province>
  <province name="贵州省">
    <city name="贵阳"></city>
    <city name="六盘水"></city>
    <city name="遵义"></city>
    <city name="安顺"></city>
    <city name="铜仁地区"></city>
    <city name="黔西南布依族苗族自治州"></city>
    <city name="毕节地区"></city>
    <city name="黔东南苗族侗族自治州"></city>
    <city name="黔南布依族苗族自治州"></city>
  </province>
  <province name="四川省">
    <city name="成都"></city>
    <city name="自贡"></city>
    <city name="攀枝花"></city>
    <city name="泸州"></city>
    <city name="德阳"></city>
    <city name="绵阳"></city>
    <city name="广元"></city>
    <city name="遂宁"></city>
    <city name="内江"></city>
    <city name="乐山"></city>
    <city name="南充"></city>
    <city name="眉山"></city>
    <city name="宜宾"></city>
    <city name="广安"></city>
    <city name="达州"></city>
    <city name="雅安"></city>
    <city name="巴中"></city>
    <city name="资阳"></city>
    <city name="阿坝藏族羌族自治州"></city>
    <city name="甘孜藏族自治州"></city>
    <city name="凉山彝族自治州"></city>
  </province>
  <province name="内蒙古自治区">
    <city name="呼和浩特"></city>
    <city name="包头"></city>
    <city name="乌海"></city>
    <city name="赤峰"></city>
    <city name="通辽"></city>
    <city name="鄂尔多斯"></city>
    <city name="呼伦贝尔"></city>
    <city name="巴彦淖尔"></city>
    <city name="乌兰察布"></city>
    <city name="兴安盟"></city>
    <city name="锡林郭勒盟"></city>
    <city name="阿拉善盟"></city>
  </province>
  <province name="宁夏回族自治区">
    <city name="银川"></city>
    <city name="石嘴山"></city>
    <city name="吴忠"></city>
    <city name="固原"></city>
    <city name="中卫"></city>
  </province>
  <province name="甘肃省">
    <city name="兰州"></city>
    <city name="嘉峪关"></city>
    <city name="金昌"></city>
    <city name="白银"></city>
    <city name="天水"></city>
    <city name="武威"></city>
    <city name="张掖"></city>
    <city name="平凉"></city>
    <city name="酒泉"></city>
    <city name="庆阳"></city>
    <city name="定西"></city>
    <city name="陇南"></city>
    <city name="临夏回族自治州"></city>
    <city name="甘南藏族自治州"></city>
  </province>
  <province name="青海省">
    <city name="西宁"></city>
    <city name="海东地区"></city>
    <city name="海北藏族自治州"></city>
    <city name="黄南藏族自治州"></city>
    <city name="海南藏族自治州"></city>
    <city name="果洛藏族自治州"></city>
    <city name="玉树藏族自治州"></city>
    <city name="海西蒙古族藏族自治州"></city>
  </province>
  <province name="西藏自治区">
    <city name="拉萨"></city>
    <city name="昌都地区"></city>
    <city name="山南地区"></city>
    <city name="日喀则地区"></city>
    <city name="那曲地区"></city>
    <city name="阿里地区"></city>
    <city name="林芝地区"></city>
  </province>
  <province name="新疆自治区">
    <city name="乌鲁木齐"></city>
    <city name="克拉玛依"></city>
    <city name="吐鲁番地区"></city>
    <city name="哈密地区"></city>
    <city name="昌吉回族自治州"></city>
    <city name="博尔塔拉蒙古自治州"></city>
    <city name="巴音郭楞蒙古自治州"></city>
    <city name="阿克苏地区"></city>
    <city name="克孜勒苏柯尔克孜自治州"></city>
    <city name="喀什地区"></city>
    <city name="和田地区"></city>
    <city name="伊犁哈萨克自治州"></city>
    <city name="塔城地区"></city>
    <city name="阿勒泰地区"></city>
    <city name="石河子"></city>
    <city name="阿拉尔"></city>
    <city name="图木舒克"></city>
    <city name="五家渠"></city>
  </province>
  <province name="安徽省">
    <city name="合肥"></city>
    <city name="芜湖"></city>
    <city name="蚌埠"></city>
    <city name="淮南"></city>
    <city name="马鞍山"></city>
    <city name="淮北"></city>
    <city name="铜陵"></city>
    <city name="安庆"></city>
    <city name="黄山"></city>
    <city name="滁州"></city>
    <city name="阜阳"></city>
    <city name="宿州"></city>
    <city name="巢湖"></city>
    <city name="六安"></city>
    <city name="亳州"></city>
    <city name="池州"></city>
    <city name="宣城"></city>
  </province>
  <province name="浙江省">
    <city name="杭州"></city>
    <city name="宁波"></city>
    <city name="温州"></city>
    <city name="嘉兴"></city>
    <city name="湖州"></city>
    <city name="绍兴"></city>
    <city name="金华"></city>
    <city name="衢州"></city>
    <city name="舟山"></city>
    <city name="台州"></city>
    <city name="丽水"></city>
  </province>
  <province name="福建省">
    <city name="福州"></city>
    <city name="厦门"></city>
    <city name="莆田"></city>
    <city name="三明"></city>
    <city name="泉州"></city>
    <city name="漳州"></city>
    <city name="南平"></city>
    <city name="龙岩"></city>
    <city name="宁德"></city>
  </province>
  <province name="台湾省">
    <city name="台北"></city>
    <city name="高雄"></city>
    <city name="基隆"></city>
    <city name="台中"></city>
    <city name="台南"></city>
    <city name="新竹"></city>
    <city name="嘉义"></city>
  </province>
  <province name="香港">
    <city name="中西区"></city>
    <city name="湾仔区"></city>
    <city name="东区"></city>
    <city name="南区"></city>
    <city name="油尖旺区"></city>
    <city name="深水?肚?></city>
    <city name="九龙城区"></city>
    <city name="黄大仙区"></city>
    <city name="观塘区"></city>
    <city name="荃湾区"></city>
    <city name="葵青区"></city>
    <city name="沙田区"></city>
    <city name="西贡区"></city>
    <city name="大埔区"></city>
    <city name="北区"></city>
    <city name="元朗区"></city>
    <city name="屯门区"></city>
    <city name="离岛区"></city>
  </province>
  <province name="澳门">
    <city name="澳门"></city>
  </province>
  <province name="其它">
    <city name="其它"></city>
  </province>
</country>

用python 代码的解析方法如下:

import xml.dom.minidom
def get_citys():
  city_xml = open(os.path.join(os.path.normpath(os.path.dirname(__file__)),'city.xml'))
  doc = xml.dom.minidom.parse(city_xml)
  citys = []
  provinces = doc.getElementsByTagName('province')
  for item in provinces:
    entry = {'province':'','citys':[]}
    province = item.getAttribute('name')
    entry['province'] = province
    for city in item.getElementsByTagName('city'):
      city = city.getAttribute('name')
      entry['citys'].append(city)
    citys.append(entry)
  return citys

这样就可以解析出数据了,其他的类似。

Python 相关文章推荐
详解python脚本自动生成需要文件实例代码
Feb 04 Python
python的构建工具setup.py的方法使用示例
Oct 23 Python
Python爬虫实例爬取网站搞笑段子
Nov 08 Python
python读取csv和txt数据转换成向量的实例
Feb 12 Python
PyCharm+Qt Designer+PyUIC安装配置教程详解
Jun 13 Python
python3.x提取中文的正则表达式示例代码
Jul 23 Python
Python代码实现http/https代理服务器的脚本
Aug 12 Python
Python循环实现n的全排列功能
Sep 16 Python
python3.7实现云之讯、聚合短信平台的短信发送功能
Sep 26 Python
python读取dicom图像示例(SimpleITK和dicom包实现)
Jan 16 Python
使用pymysql查询数据库,把结果保存为列表并获取指定元素下标实例
May 15 Python
Python Selenium操作Cookie的实例方法
Feb 28 Python
对pyqt5中QTabWidget的相关操作详解
Jun 21 #Python
python实现得到当前登录用户信息的方法
Jun 21 #Python
python-django中的APPEND_SLASH实现方法
Jun 21 #Python
Python2.7版os.path.isdir中文路径返回false的解决方法
Jun 21 #Python
pyqt5与matplotlib的完美结合实例
Jun 21 #Python
django框架防止XSS注入的方法分析
Jun 21 #Python
pyqt5中QThread在使用时出现重复emit的实例
Jun 21 #Python
You might like
星际争霸教主Flash的ID由来:你永远不会知道他之前的ID是www!
2019/01/18 星际争霸
php 向访客和爬虫显示不同的内容
2009/11/09 PHP
PHP实现手机归属地查询API接口实现代码
2012/08/27 PHP
PHP实现上传文件并存进数据库的方法
2015/07/16 PHP
PHP定时执行任务的3种方法详解
2015/12/21 PHP
使用git迁移Laravel项目至新开发环境的步骤详解
2020/04/06 PHP
动态加载jquery库的方法
2014/02/12 Javascript
scrollWidth,clientWidth,offsetWidth的区别
2015/01/13 Javascript
js实现的万能flv网页播放器代码
2016/04/30 Javascript
Backbone中View之间传值的学习心得
2016/08/09 Javascript
vue插件tab选项卡使用小结
2016/10/27 Javascript
详解JavaScript中的属性和特性
2016/12/08 Javascript
Bootstrap table两种分页示例
2016/12/23 Javascript
JavaScript学习笔记之函数记忆
2017/09/06 Javascript
JS实现select选中option触发事件操作示例
2018/07/13 Javascript
理解JavaScript中的Proxy 与 Reflection API
2020/09/21 Javascript
Webpack3+React16代码分割的实现
2021/03/03 Javascript
python检测是文件还是目录的方法
2015/07/03 Python
Django实现简单分页功能的方法详解
2017/12/05 Python
你真的了解Python的random模块吗?
2017/12/12 Python
Python实现的NN神经网络算法完整示例
2018/06/19 Python
如何利用python制作时间戳转换工具详解
2018/09/12 Python
python pandas读取csv后,获取列标签的方法
2018/11/12 Python
通过字符串导入 Python 模块的方法详解
2019/10/27 Python
Django 自定义分页器的实现代码
2019/11/24 Python
python字符串拼接+和join的区别详解
2020/12/03 Python
python 6种方法实现单例模式
2020/12/15 Python
企业治理工作自我评价
2013/09/26 职场文书
青年教师培训方案
2014/02/06 职场文书
校车安全责任书
2014/08/25 职场文书
公安机关纪律作风整顿剖析
2014/10/10 职场文书
南京导游词
2015/02/03 职场文书
学年个人总结范文
2015/03/05 职场文书
清明节寄语2015
2015/03/23 职场文书
公司员工培训管理制度
2015/08/04 职场文书
2016年春季开学典礼新闻稿
2015/11/25 职场文书