Python利用ElementTree模塊處理XML的方法詳解
前言
最近因為工作的需要,在使用 Python 來發(fā)送 SOAP 請求以測試 Web Service 的性能,由于 SOAP 是基于 XML 的,故免不了需要使用 python 來處理 XML 數據。在對比了幾種方案后,最后選定使用 xml.etree.ElementTree
模塊來實現。
這篇文章記錄了使用 xml.etree.ElementTree
模塊常用的幾個操作,也算是總結一下,免得以后忘記了。分享出來也方法需要的朋友們參考學習,下面話不多說了,來一起看看詳細的介紹吧。
概述
對比其他 Python 處理 XML 的方案,xml.etree.ElementTree
模塊(下文我們以 ET 來表示)相對來說比較簡單,接口也較友好。
官方文檔 里面對 ET 模塊進行了較為詳細的描述,總的來說,ET 模塊可以歸納為三個部分:ElementTree類,Element類以及一些操作 XML 的函數。
XML 可以看成是一種樹狀結構,ET 使用ElementTree類來表示整個 XML 文檔,使用Element類來表示 XML 的一個結點。對整 XML 文檔的操作一般是對ElementTree對象進行,而對 XML 結點的操作一般是對Element對象進行。
解析 XML 文件
ET 模塊支持從一個 XML 文件構造ElementTree對象,例如我們的 XML 文件example.xml內容如下(下文會繼續(xù)使用這個 XML 文檔):
<?xml version="1.0" encoding="utf-8"?> <data> <country name="Liechtenstein"> <rank>1</rank> <year>2008</year> <gdppc>141100</gdppc> <neighbor name="Austria" direction="E"/> <neighbor name="Switzerland" direction="W"/> </country> <country name="Singapore"> <rank>4</rank> <year>2011</year> <gdppc>59900</gdppc> <neighbor name="Malaysia" direction="N"/> </country> </data>
可以使用 ET 模塊的parse()
函數來從指定的 XML 文件構造一個ElementTree對象:
import xml.etree.ElementTree as ET # 獲取 XML 文檔對象 ElementTree tree = ET.parse('example.xml') # 獲取 XML 文檔對象的根結點 Element root = tree.getroot() # 打印根結點的名稱 print root.tag
從 XML 文件構造好ElementTree對象后,還可以獲取其結點,或者再繼續(xù)對結點進行進一步的操作。
解析 XML 字符串
ET 模塊的fromstring()函數提供從 XML 字符串構造一個Element對象的功能。
xml_str = ET.tostring(root) print xml_str root = ET.fromstring(xml_str) print root.tag
接著上面的代碼,我們使用 ET 模塊的tostring()
函數來將上面我們構造的root對象轉化為字符串,然后使用fromstring()
函數重新構造一個Element對象,并賦值給root變量,這時root代表整個 XML 文檔的根結點。
構造 XML
如果我們需要構造 XML 文檔,可以使用 ET 模塊的 Element類以及SubElement()
函數。
可以使用Element類來生成一個Element對象作為根結點,然后使用ET.SubElement()
函數生成子結點。
a = ET.Element('a') b = ET.SubElement(a, 'b') b.text = 'leehao.me' c = ET.SubElement(a, 'c') c.attrib['greeting'] = 'hello' d = ET.SubElement(a, 'd') d.text = 'www.leehao.me' xml_str = ET.tostring(a, encoding='UTF-8') print xml_str
輸出:
<?xml version='1.0' encoding='UTF-8'?> <a><b>leehao.me</b><c greeting="hello" /><d>www.leehao.me</d></a>
如果需要輸出到文件中,可以繼續(xù)使用ElementTree.write()
方法來處理:
# 先構造一個 ElementTree 以便使用其 write 方法 tree = ET.ElementTree(a) tree.write('a.xml', encoding='UTF-8')
執(zhí)行后,便會生成一個 XML 文件a.xml:
<?xml version='1.0' encoding='UTF-8'?> <a><b>leehao.me</b><c greeting="hello" /><d>www.leehao.me</d></a>
XML 結點的查找與更新
1. 查找 XML 結點
Element類提供了Element.iter()
方法來查找指定的結點。Element.iter()
會遞歸查找所有的子結點,以便查找到所有符合條件的結點。
# 獲取 XML 文檔對象 ElementTree tree = ET.parse('example.xml') # 獲取 XML 文檔對象的根結點 Element root = tree.getroot() # 遞歸查找所有的 neighbor 子結點 for neighbor in root.iter('neighbor'): print neighbor.attrib
輸出:
{'direction': 'E', 'name': 'Austria'} {'direction': 'W', 'name': 'Switzerland'} {'direction': 'N', 'name': 'Malaysia'}
如果使用Element.findall()
或者Element.find()
方法,則只會從結點的直接子結點中查找,并不會遞歸查找。
for country in root.findall('country'): rank = country.find('rank').text name = country.get('name') print name, rank
輸出:
Liechtenstein 1 Singapore 4
2. 更新結點
如果需要更新結點的文本,可以通過直接修改Element.text
來實現。如果需要更新結點的屬性,可以通過直接修改Element.attrib
來實現。
對結點進行更新后,可以使用ElementTree.write()
方法將更新后的 XML 文檔寫入文件中。
# 獲取 XML 文檔對象 ElementTree tree = ET.parse('example.xml') # 獲取 XML 文檔對象的根結點 Element root = tree.getroot() for rank in root.iter('rank'): new_rank = int(rank.text) + 1 rank.text = str(new_rank) rank.attrib['updated'] = 'yes' tree.write('output.xml', encoding='UTF-8')
新生成的output.xml文件以下:
<?xml version='1.0' encoding='UTF-8'?> <data> <country name="Liechtenstein"> <rank updated="yes">2</rank> <year>2008</year> <gdppc>141100</gdppc> <neighbor direction="E" name="Austria" /> <neighbor direction="W" name="Switzerland" /> </country> <country name="Singapore"> <rank updated="yes">5</rank> <year>2011</year> <gdppc>59900</gdppc> <neighbor direction="N" name="Malaysia" /> </country> </data>
對比example.xml文件,可以看到output.xml文件已更新。
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
參考資料
相關文章
Python源碼學習之PyObject和PyTypeObject
今天給大家?guī)淼氖顷P于Python源碼的相關知識學習,文章圍繞著PyObject和PyTypeObject展開,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下2021-06-06Pandas groupby apply agg 的區(qū)別 運行自定義函數說明
這篇文章主要介紹了Pandas groupby apply agg 的區(qū)別 運行自定義函數說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03