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 相关文章推荐
Windows下PyMongo下载及安装教程
Apr 27 Python
Python实现简单登录验证
Apr 13 Python
python之Socket网络编程详解
Sep 29 Python
Python自定义装饰器原理与用法实例分析
Jul 16 Python
python url 参数修改方法
Dec 26 Python
详解Python3网络爬虫(二):利用urllib.urlopen向有道翻译发送数据获得翻译结果
May 07 Python
windows下安装Python虚拟环境virtualenvwrapper-win
Jun 14 Python
Python OpenCV中的resize()函数的使用
Jun 20 Python
Python GUI库PyQt5图形和特效样式QSS介绍
Feb 25 Python
Python实现对adb命令封装
Mar 06 Python
Django中使用Celery的方法步骤
Dec 07 Python
如何用Matlab和Python读取Netcdf文件
Feb 19 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
php文件上传的简单实例
2013/10/19 PHP
在PHP站点的页面上添加Facebook评论插件的实例教程
2016/01/08 PHP
Javascript 构造函数,公有,私有特权和静态成员定义方法
2009/11/30 Javascript
javascript全局变量封装模块实现代码
2012/11/28 Javascript
jQuery操作checkbox选择(list/table)
2013/04/07 Javascript
js图片延迟加载的实现方法及思路
2013/07/22 Javascript
Jquery 过滤器(first,last,not,even,odd)的使用
2014/01/22 Javascript
JavaScript中实现map功能代码分享
2015/06/11 Javascript
Jquery promise实现一张一张加载图片
2015/11/13 Javascript
jQuery点击输入框显示验证码图片
2016/05/19 Javascript
JavaScript中的工厂函数(推荐)
2017/03/08 Javascript
react开发中如何使用require.ensure加载es6风格的组件
2017/05/09 Javascript
详解webpack异步加载业务模块
2017/06/23 Javascript
js全屏事件fullscreenchange 实现全屏、退出全屏操作
2019/09/17 Javascript
vue实现修改图片后实时更新
2019/11/14 Javascript
[01:00:53]OG vs IG 2018国际邀请赛小组赛BO2 第一场 8.18
2018/08/19 DOTA
优化Python代码使其加快作用域内的查找
2015/03/30 Python
Python编程之字符串模板(Template)用法实例分析
2017/07/22 Python
Python内置函数delattr的具体用法
2017/11/23 Python
Python3计算三角形的面积代码
2017/12/18 Python
tensorflow使用神经网络实现mnist分类
2018/09/08 Python
python+selenium 点击单选框-radio的实现方法
2019/09/03 Python
浅谈Pycharm最有必要改的几个默认设置项
2020/02/14 Python
Django 解决distinct无法去除重复数据的问题
2020/05/20 Python
Python flask框架实现查询数据库并显示数据
2020/06/04 Python
HTML5实践-图片设置成灰度图
2012/11/12 HTML / CSS
JoJo Maman Bébé爱尔兰官网:英国最受欢迎的精品母婴品牌
2020/12/20 全球购物
销售目标责任书
2014/07/23 职场文书
影视广告专业求职信
2014/09/02 职场文书
论群众路线学习心得体会
2014/10/31 职场文书
幼儿园小班个人工作总结
2015/02/12 职场文书
自主招生专家推荐信
2015/03/26 职场文书
手机销售员岗位职责
2015/04/11 职场文书
有关浪费资源的建议书
2015/09/14 职场文书
laravel ajax curd 搜索登录判断功能的实现
2021/04/17 PHP
mysql聚集索引、辅助索引、覆盖索引、联合索引的使用
2022/02/12 MySQL