1. 程式人生 > 其它 >Python中xml.etree.ElementTree讀寫xml檔案例項

Python中xml.etree.ElementTree讀寫xml檔案例項

import os
import xml.etree.ElementTree as ET

'''
Python 標準庫中,提供了6種可以用於處理XML的包,本文舉例項說明第6種
1.xml.dom
2.xml.dom.minidom
3.xml.dom.pulldom
4.xml.sax
5.xml.parse.expat
6.xml.etree.ElementTree(簡稱ET)

xml 檔案的內容
<user>
<name title="xml example">It is an xml example</name>
<article name="My article">
<algorithm name="a1">
<interval name="n1">1000</interval>
</algorithm>
</article>
<article name="Your article">
<algorithm name="a2">
<interval name="n2">1001</interval>
</algorithm>
</article>
<profile>
<profile-name>profile_hard_easy</profile-name>
<level>
<level-value>1</level-value>
<level-type>hard</level-type>
</level>
<level>
<level-value>2</level-value>
<level-type>easy</level-type>
</level>
</profile>
</user>



#獲取當前檔案所在目錄
print(os.path.dirname(__file__))
#路徑拼接,得到xml 檔案的路徑
dirpath = os.path.join(os.path.dirname(__file__),"test2.xml")

#用ET.ElementTree 讀取xml檔案,將xml檔案解析為tree
'''
ET提供了兩個物件ElementTree將整個XML文件轉化為樹, Element則代表著樹上的單個節點。對整個XML文件的互動(讀取,寫入,查詢需要的元素),一般是在 ElementTree層面進行的。對單個XML元素及其子元素,則是在 Element層面進行的。
'''
tree = ET.ElementTree(file=dirpath)
#獲取xml 檔案的根結點
root = tree.getroot()
#xml 的根結點是一個Element 物件.
'''
Element 物件常用的屬性有:
Element.tag --輸出標籤的名稱
Element.text --輸出標籤包含的內容
Element.attrib --標籤的屬性
'''
print(root) #輸出: <Element 'user' at 0x000001C02A2DB818>
#檢視xml根結點root的屬性
print(root.tag,root.attrib) #輸出: user {}, 根結點沒有屬性

#可以通過遞迴方式,遍歷根結點的所有子元素,獲取數中所有的元素
for child_root in root:
print(child_root.tag,child_root.text,child_root.attrib)
'''
輸出:
name
{}
article
{'name': 'My article'}
profile
{}
'''
#根據索引訪問特定的子元素
print(root[1].tag,root[1].text,root[1].attrib)
'''
輸出:
article
{'name': 'My article'}
'''

#使用ElementTree iter() 方法遍歷 tree的所有元素
for elem in root.iter():
print(elem.tag,elem.attrib)
if elem.tag == "profile":
print("=========Find it, I can do what I want")

#使用ElementTree iter() 方法,遍歷指定tag的元素,找到自己感興趣的屬性

for sub_elem in root.iter(tag="article"):
print(sub_elem.tag,sub_elem.attrib)

#通過XPath 查詢元素

'''
ElementTree 物件常用到的方法如下,它們的相同點是接收Xpath路徑作為引數,不同點是
1. find 返回第一個匹配的子元素
2. findall 以列表的形式返回所有匹配的子元素
3. iterfind 則返回一個所有匹配元素的迭代器(iterator)

Xpath 使用例子:
1. 返回所有tag為article的元素:
findall("article")
2. 返回 tag為article下之下所有tag為algorithm的元素,注意格式必須為 父節點/一代子節點,而不能為父節點/二代子節點,比如article/interval 就會找不到
findall(article/algorithm)

'''
'''
1. find 輸出:
algorithm
{}
'''
for elem in root.find("article"):
print(elem.tag,elem.text,elem.attrib)

'''
2. findall 和 iterfind 輸出:
article
{'name': 'My article'}
article
{'name': 'Your article'}
'''
for elem in root.findall("article"):
print(elem.tag,elem.text,elem.attrib)

for elem in root.findall("article"):
print(elem.tag,elem.text,elem.attrib)


#生成和更改xml 檔案

'''
ElementTree物件的write方法可以往xml寫入內容
'''
#為根節點的第一個子元素增加屬性 color=red
print(root[0].tag,root[0].attrib) #name {'title': 'xml example'}
root[0].set("color","red")
print(root[0].tag,root[0].attrib) #name {'title': 'xml example', 'color': 'red'}

#刪掉根節點的其中一個子元素
i=0
for item in root:
print(i,item.tag)
i=i+1
del root[4] #刪掉最後一個子元素 foot
i=0
for item in root:
print(i,item.tag)
i=i+1

#將修改後的檔案寫入到一個新的檔案 test3.xml,新文件的元素屬性順序與原文件不同。這是因為ET是以字典的形式儲存屬性的,而字典是一個無序的資料結構。同時 XML也不關注屬性的順序,故亂序的影響不大
tree.write(os.path.join(os.path.dirname(__file__),"test3.xml"))

#構建一個新的xml檔案
#生成根元素
root=ET.Element("root")
#生成子元素 A
a=ET.Element("A")
#增加元素A的子元素 child1
a_child=ET.SubElement(a,"child1")
a_child.text="I'm child of A"
#增加元素A的子元素 child2
a_child1=ET.SubElement(a,"child2")
#生成子元素B
b=ET.Element("B")
#增加元素B的子元素child1
b_child=ET.SubElement(b,"child1")
b_child.text="I'm child of B"
b_child.set("name","book") #set() 接收的是 key,value 形式

#將a和b 組成一個元組傳入extend()方法中,元素 A和B作為根元素的子元素
root.extend((a,b))
trees=ET.ElementTree(root)
#將trees 寫入到檔案 test4.xml, 內容為 <root><A><child1>I'm child of A</child1><child2 /></A><B><child1 name="book">I'm child of B</child1></B></root>
trees.write(os.path.join(os.path.dirname(__file__),"test4.xml"))