python操作xml文件詳細(xì)介紹
關(guān)于python讀取xml文章很多,但大多文章都是貼一個(gè)xml文件,然后再貼個(gè)處理文件的代碼。這樣并不利于初學(xué)者的學(xué)習(xí),希望這篇文章可以更通俗易懂的教如何使用python 來讀取xml 文件。
一、什么是xml?
xml即可擴(kuò)展標(biāo)記語(yǔ)言,它可以用來標(biāo)記數(shù)據(jù)、定義數(shù)據(jù)類型,是一種允許用戶對(duì)自己的標(biāo)記語(yǔ)言進(jìn)行定義的源語(yǔ)言。
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>測(cè)試</caption>
</item>
</login>
<item id="2">
<caption>Zope</caption>
</item>
</catalog>
Ok ,從結(jié)構(gòu)上,它很像我們常見的HTML超文本標(biāo)記語(yǔ)言。但他們被設(shè)計(jì)的目的是不同的,超文本標(biāo)記語(yǔ)言被設(shè)計(jì)用來顯示數(shù)據(jù),其焦點(diǎn)是數(shù)據(jù)的外觀。它被設(shè)計(jì)用來傳輸和存儲(chǔ)數(shù)據(jù),其焦點(diǎn)是數(shù)據(jù)的內(nèi)容。
那么它有如下特征:
首先,它是有標(biāo)簽對(duì)組成,<aa></aa>
標(biāo)簽可以有屬性:<aa id='123'></aa>
標(biāo)簽對(duì)可以嵌入數(shù)據(jù):<aa>abc</aa>
標(biāo)簽可以嵌入子標(biāo)簽(具有層級(jí)關(guān)系):
二、獲得標(biāo)簽屬性
那么,下面來介紹如何用python來讀取這種類型的文件。
#coding=utf-8
import xml.dom.minidom
#打開xml文檔
dom = xml.dom.minidom.parse('abc.xml')
#得到文檔元素對(duì)象
root = dom.documentElement
print root.nodeName
print root.nodeValue
print root.nodeType
print root.ELEMENT_NODE
mxl.dom.minidom 模塊被用來處理xml文件,所以要先引入。
xml.dom.minidom.parse() 用于打開一個(gè)xml文件,并將這個(gè)文件對(duì)象dom變量。
documentElement 用于得到dom對(duì)象的文檔元素,并把獲得的對(duì)象給root
每一個(gè)結(jié)點(diǎn)都有它的nodeName,nodeValue,nodeType屬性。
nodeName為結(jié)點(diǎn)名字。
nodeValue是結(jié)點(diǎn)的值,只對(duì)文本結(jié)點(diǎn)有效。
nodeType是結(jié)點(diǎn)的類型。catalog是ELEMENT_NODE類型
現(xiàn)在有以下幾種:
'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'
三、獲得子標(biāo)簽
現(xiàn)在要獲得catalog的子標(biāo)簽以的標(biāo)簽name
<?xml version="1.0" encoding="utf-8"?>
<catalog>
<maxid>4</maxid>
<login username="pytest" passwd='123456'>
<caption>Python</caption>
<item id="4">
<caption>測(cè)試</caption>
</item>
</login>
<item id="2">
<caption>Zope</caption>
</item>
</catalog>
對(duì)于知道元素名字的子元素,可以使用getElementsByTagName方法獲?。?br />
#coding=utf-8
import xml.dom.minidom
#打開xml文檔
dom = xml.dom.minidom.parse('abc.xml')
#得到文檔元素對(duì)象
root = dom.documentElement
bb = root.getElementsByTagName('maxid')
b= bb[0]
print b.nodeName
bb = root.getElementsByTagName('login')
b= bb[0]
print b.nodeName
如何區(qū)分相同標(biāo)簽名字的標(biāo)簽:
<?xml version="1.0" encoding="utf-8"?>
<catalog>
<maxid>4</maxid>
<login username="pytest" passwd='123456'>
<caption>Python</caption>
<item id="4">
<caption>測(cè)試</caption>
</item>
</login>
<item id="2">
<caption>Zope</caption>
</item>
</catalog>
<caption>和<item>標(biāo)簽不止一個(gè)如何區(qū)分?
#coding=utf-8
import xml.dom.minidom
#打開xml文檔
dom = xml.dom.minidom.parse('abc.xml')
#得到文檔元素對(duì)象
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') 獲得的是標(biāo)簽為caption 一組標(biāo)簽,b[0]表示一組標(biāo)簽中的第一個(gè);b[2] ,表示這一組標(biāo)簽中的第三個(gè)。
四、獲得標(biāo)簽屬性值
<?xml version="1.0" encoding="utf-8"?>
<catalog>
<maxid>4</maxid>
<login username="pytest" passwd='123456'>
<caption>Python</caption>
<item id="4">
<caption>測(cè)試</caption>
</item>
</login>
<item id="2">
<caption>Zope</caption>
</item>
</catalog>
<login>和<item>標(biāo)簽是有屬性的,如何獲得他們的屬性?
#coding=utf-8
import xml.dom.minidom
#打開xml文檔
dom = xml.dom.minidom.parse('abc.xml')
#得到文檔元素對(duì)象
root = dom.documentElement
itemlist = root.getElementsByTagName('login')
item = itemlist[0]
un=item.getAttribute("username")
print un
pd=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方法可以獲得元素的屬性所對(duì)應(yīng)的值。
五、獲得標(biāo)簽對(duì)之間的數(shù)據(jù)
<?xml version="1.0" encoding="utf-8"?>
<catalog>
<maxid>4</maxid>
<login username="pytest" passwd='123456'>
<caption>Python</caption>
<item id="4">
<caption>測(cè)試</caption>
</item>
</login>
<item id="2">
<caption>Zope</caption>
</item>
</catalog>
<caption>標(biāo)簽對(duì)之間是有數(shù)據(jù)的,如何獲得這些數(shù)據(jù)?
獲得標(biāo)簽對(duì)之間的數(shù)據(jù)有多種方法,
方法一:
#coding=utf-8
import xml.dom.minidom
#打開xml文檔
dom = xml.dom.minidom.parse('abc.xml')
#得到文檔元素對(duì)象
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 屬性返回被選節(jié)點(diǎn)的第一個(gè)子節(jié)點(diǎn),.data表示獲取該節(jié)點(diǎn)人數(shù)據(jù)。
方法二:
#coding=utf-8
from xml.etree import ElementTree as ET
per=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
方法二有點(diǎn)復(fù)雜,所引用模塊也與前面的不一樣,findall用于指定在哪一級(jí)標(biāo)簽下開始遍歷。
getchildren方法按照文檔順序返回所有子標(biāo)簽。并輸出標(biāo)簽名(child.tag)和標(biāo)簽的數(shù)據(jù)(child.text)
其實(shí),方法二的作用不在于此,它核心功能是可以遍歷某一級(jí)標(biāo)簽下的所有子標(biāo)簽。
PS:這里再為大家提供幾款關(guān)于xml操作的在線工具供大家參考使用:
在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson
在線格式化XML/在線壓縮XML:
http://tools.jb51.net/code/xmlformat
XML在線壓縮/格式化工具:
http://tools.jb51.net/code/xml_format_compress
相關(guān)文章
Python靜態(tài)類型檢查新工具之pyright 使用指南
這篇文章主要介紹了Python靜態(tài)類型檢查新工具之pyright 使用指南,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04
Python中內(nèi)置函數(shù)append()、extend()的用法及區(qū)別詳解
這篇文章主要介紹了Python中內(nèi)置函數(shù)append()、extend()的用法及區(qū)別,還探討了append()函數(shù)添加列表時(shí)發(fā)生的同步變化問題,并提供了解決方案,需要的朋友可以參考下2025-03-03
python pandas 對(duì)時(shí)間序列文件處理的實(shí)例
今天小編就為大家分享一篇python pandas 對(duì)時(shí)間序列文件處理的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-06-06
Django 全局的static和templates的使用詳解
這篇文章主要介紹了Django 全局的static和templates的使用詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07
python實(shí)現(xiàn)從字符串中找出字符1的位置以及個(gè)數(shù)的方法
這篇文章主要介紹了python實(shí)現(xiàn)從字符串中找出字符1的位置以及個(gè)數(shù)的方法,對(duì)于Python字符串操作的學(xué)習(xí)有一定的幫助與借鑒作用,需要的朋友可以參考下2014-08-08

