亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

python解析xml簡單示例

發布時間:2020-09-06 13:01:39 來源:腳本之家 閱讀:168 作者:輕舞肥羊 欄目:開發技術

本文實例講述了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

這樣就可以解析出數據了,其他的類似。

PS:這里再為大家提供幾款關于xml操作的在線工具供大家參考使用:

在線XML/JSON互相轉換工具:
http://tools.jb51.net/code/xmljson

在線格式化XML/在線壓縮XML
http://tools.jb51.net/code/xmlformat

XML在線壓縮/格式化工具:
http://tools.jb51.net/code/xml_format_compress

XML代碼在線格式化美化工具:
http://tools.jb51.net/code/xmlcodeformat

更多關于Python相關內容感興趣的讀者可查看本站專題:《Python操作xml數據技巧總結》、《Python數據結構與算法教程》、《Python Socket編程技巧總結》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總》

希望本文所述對大家Python程序設計有所幫助。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

仁化县| 冀州市| 定兴县| 宜都市| 浏阳市| 会昌县| 黄骅市| 陵川县| 曲松县| 嘉善县| 大洼县| 浪卡子县| 都昌县| 宣武区| 库尔勒市| 开封县| 博罗县| 聂拉木县| 敖汉旗| 五河县| 望谟县| 青海省| 乐亭县| 三河市| 额尔古纳市| 集安市| 建阳市| 灵丘县| 吴旗县| 宜都市| 二手房| 江口县| 昆山市| 明星| 珠海市| 涟水县| 疏附县| 什邡市| 凤阳县| 丘北县| 福海县|