關于python讀取xml文章很多,但大多文章都是貼一個xml文件,然后再貼個處理文件的代碼。這樣并不利于初學者的學習,希望這篇文章可以更通俗易懂的教如何使用python 來讀取xml 文件。
一、什么是xml?
xml即可擴展標記語言,它可以用來標記數據、定義數據類型,是一種允許用戶對自己的標記語言進行定義的源語言。
abc.xml
復制代碼 代碼如下:<?xml version="1.0" encoding="utf-8"?><catalog> <maxid>4</maxid> <login username="pytest" passwd='123456'> <caption>Python</caption> <item id="4"> <caption>測試</caption> </item> </login> <item id="2"> <caption>Zope</caption> </item></catalog>Ok ,從結構上,它很像我們常見的HTML超文本標記語言。但他們被設計的目的是不同的,超文本標記語言被設計用來顯示數據,其焦點是數據的外觀。它被設計用來傳輸和存儲數據,其焦點是數據的內容。
那么它有如下特征:
首先,它是有標簽對組成,<aa></aa>
標簽可以有屬性:<aa id='123'></aa>
標簽對可以嵌入數據:<aa>abc</aa>
標簽可以嵌入子標簽(具有層級關系):
二、獲得標簽屬性
那么,下面來介紹如何用python來讀取這種類型的文件。
復制代碼 代碼如下:#coding=utf-8import xml.dom.minidom#打開xml文檔dom = xml.dom.minidom.parse('abc.xml')
#得到文檔元素對象root = dom.documentElementPRint root.nodeNameprint root.nodeValueprint root.nodeTypeprint root.ELEMENT_NODE
mxl.dom.minidom 模塊被用來處理xml文件,所以要先引入。
xml.dom.minidom.parse() 用于打開一個xml文件,并將這個文件對象dom變量。
documentElement 用于得到dom對象的文檔元素,并把獲得的對象給root
每一個結點都有它的nodeName,nodeValue,nodeType屬性。
nodeName為結點名字。
nodeValue是結點的值,只對文本結點有效。
nodeType是結點的類型。catalog是ELEMENT_NODE類型
現在有以下幾種:
'ATTRIBUTE_NODE''CDATA_SECTION_NODE''COMMENT_NODE''DOCUMENT_FRAGMENT_NODE''DOCUMENT_NODE''DOCUMENT_TYPE_NODE''ELEMENT_NODE''ENTITY_NODE''ENTITY_REFERENCE_NODE''NOTATION_NODE''PROCESSING_INSTRUCTION_NODE''TEXT_NODE'
三、獲得子標簽
現在要獲得catalog的子標簽以的標簽name
復制代碼 代碼如下:<?xml version="1.0" encoding="utf-8"?><catalog> <maxid>4</maxid> <login username="pytest" passwd='123456'> <caption>Python</caption> <item id="4"> <caption>測試</caption> </item> </login> <item id="2"> <caption>Zope</caption> </item></catalog>對于知道元素名字的子元素,可以使用getElementsByTagName方法獲取:
復制代碼 代碼如下:#coding=utf-8import xml.dom.minidom#打開xml文檔dom = xml.dom.minidom.parse('abc.xml')
#得到文檔元素對象root = dom.documentElement
bb = root.getElementsByTagName('maxid')b= bb[0]print b.nodeName
bb = root.getElementsByTagName('login')b= bb[0]print b.nodeName
如何區分相同標簽名字的標簽:
復制代碼 代碼如下:<?xml version="1.0" encoding="utf-8"?><catalog> <maxid>4</maxid> <login username="pytest" passwd='123456'> <caption>Python</caption> <item id="4"> <caption>測試</caption> </item> </login> <item id="2"> <caption>Zope</caption> </item></catalog><caption>和<item>標簽不止一個如何區分?
復制代碼 代碼如下:#coding=utf-8import xml.dom.minidom#打開xml文檔dom = xml.dom.minidom.parse('abc.xml')
#得到文檔元素對象root = dom.documentElement
bb = root.getElementsByTagName('caption')b= bb[2]print b.nodeName
bb = root.getElementsByTagName('item')b= bb[1]print b.nodeName
root.getElementsByTagName('caption') 獲得的是標簽為caption 一組標簽,b[0]表示一組標簽中的第一個;b[2] ,表示這一組標簽中的第三個。
四、獲得標簽屬性值
復制代碼 代碼如下:<?xml version="1.0" encoding="utf-8"?><catalog> <maxid>4</maxid> <login username="pytest" passwd='123456'> <caption>Python</caption> <item id="4"> <caption>測試</caption> </item> </login> <item id="2"> <caption>Zope</caption> </item></catalog><login>和<item>標簽是有屬性的,如何獲得他們的屬性?
復制代碼 代碼如下:#coding=utf-8import xml.dom.minidom#打開xml文檔dom = xml.dom.minidom.parse('abc.xml')
#得到文檔元素對象root = dom.documentElement
itemlist = root.getElementsByTagName('login')item = itemlist[0]un=item.getAttribute("username")print unpd=item.getAttribute("passwd")print pd
ii = root.getElementsByTagName('item')i1 = ii[0]i=i1.getAttribute("id")print i
i2 = ii[1]i=i2.getAttribute("id")print i
getAttribute方法可以獲得元素的屬性所對應的值。
五、獲得標簽對之間的數據
復制代碼 代碼如下:<?xml version="1.0" encoding="utf-8"?><catalog> <maxid>4</maxid> <login username="pytest" passwd='123456'> <caption>Python</caption> <item id="4"> <caption>測試</caption> </item> </login> <item id="2"> <caption>Zope</caption> </item></catalog><caption>標簽對之間是有數據的,如何獲得這些數據?
獲得標簽對之間的數據有多種方法,
方法一:
復制代碼 代碼如下:#coding=utf-8import xml.dom.minidom#打開xml文檔dom = xml.dom.minidom.parse('abc.xml')
#得到文檔元素對象root = dom.documentElement
cc=dom.getElementsByTagName('caption')c1=cc[0]print c1.firstChild.data
c2=cc[1]print c2.firstChild.data
c3=cc[2]print c3.firstChild.data
firstChild 屬性返回被選節點的第一個子節點,.data表示獲取該節點人數據。
方法二:
復制代碼 代碼如下:#coding=utf-8from xml.etree import ElementTree as ETper=ET.parse('abc.xml')p=per.findall('./login/item')for oneper in p: for child in oneper.getchildren(): print child.tag,':',child.text
p=per.findall('./item')
for oneper in p: for child in oneper.getchildren(): print child.tag,':',child.text
方法二有點復雜,所引用模塊也與前面的不一樣,findall用于指定在哪一級標簽下開始遍歷。
getchildren方法按照文檔順序返回所有子標簽。并輸出標簽名(child.tag)和標簽的數據(child.text)
其實,方法二的作用不在于此,它核心功能是可以遍歷某一級標簽下的所有子標簽。
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
新聞熱點
疑難解答