1. 程式人生 > 實用技巧 >python xml.etree.ElementTree模組生成、解析xml

python xml.etree.ElementTree模組生成、解析xml

一,XML檔案格式介紹

<tag attrib = > text </tag> tail
例:<APP_KEY channel = 'CSDN'> hello123456789 </APP_KEY>
- tag,即標籤,用於標識該元素表示哪種資料,即APP_KEY
- attrib,即屬性,用Dictionary形式儲存,即{‘channel’ = ‘CSDN’}
- text,文字字串,可以用來儲存一些資料,即hello123456789
- tail,尾字串,並不是必須的。

二,示例

'''
在Python標準庫中,ElementTree有兩種實現方式:一種是純Python的實現xml.etree.ElementTree,另一種是速度更快一點的xml.etree.cElementTree。如果不確定環境中是否有cElementTree,可以使用如下的方式匯入
但從Python 3.3開始,會預設使用cElementTree來加快速度,但是之前的版本最好使用如上的程式碼,以提高程式碼的相容性。
''' try: import xml.etree.cElementTree as ET except ImportError: import xml.etree.ElementTree as ET def makeXML(): # 建立一個根節點,標籤名為Root root_element = ET.Element("Root") # 插入兩個子節點 sub_element1 = ET.SubElement(root_element, "sub_elemtnt1") sub_element2 = ET.SubElement(root_element, "
sub_elemtnt2") for i in range(5): # 設定tag 以及 subb = ET.SubElement(sub_element1, f"name_{i}", tag=f"tag_{i}", attrib={ "name": f"value_{i}"}) # 為標籤賦值 subb.text = f"abc{i}" subb2 = ET.SubElement(sub_element2, f"name_{i}", tag=f"
tag_{i}", attrib={ "name": f"value_{i}"}) subb2.text = f"efg{i}" # 建立xml樹,並將根節點放入其中 xml_tree = ET.ElementTree(root_element) # 將xml 寫入到檔案中,引數1:xml檔案生成的位置和名字,引數2:指定xml編碼,引數3:xml 宣言,
# 即:是否有 <?xml version='1.0' encoding='utf-8'?> (這個方法生成的xml檔案使用文字開啟格式是沒有縮排的)
xml_tree.write(r"C:\Users\v-yunhgu\Desktop\example.xml", encoding="utf-8", xml_declaration=True) # 第二種寫入檔案的方法 (這個方法生成的xml檔案使用文字開啟格式是有縮排的) # ET模組轉換root為bytes輸出 xml_string = ET.tostring(root_element) from xml.dom import minidom dom = minidom.parseString(xml_string) with open(r"C:\Users\v-yunhgu\Desktop\example2.xml", 'w', encoding='utf-8') as f: # indent為根節點縮排,newl每行資料句末符號,addindent為其他節點縮排 dom.writexml(f, indent='\t', newl='\n', addindent='\t', encoding='utf-8') def getXML(): xmlPath = r"C:\Users\v-yunhgu\Desktop\example2.xml" # 解析xml 檔案 xml_tree = ET.parse(xmlPath) # 獲取根節點 root_element = xml_tree.getroot() print("根節點名字:", root_element.tag) # 遍歷根節點下的子節點 for sub in root_element: print("根節點下的位元組點:", sub.tag) for s in sub: print(s.tag, s.attrib, s.text) if __name__ == "__main__": makeXML() getXML()

結果:

example.xml

<?xml version='1.0' encoding='utf-8'?>
<Root><sub_elemtnt1><name_0 name="value_0" tag="tag_0">abc0</name_0><name_1 name="value_1" tag="tag_1">abc1</name_1><name_2 name="value_2" tag="tag_2">abc2</name_2><name_3 name="value_3" tag="tag_3">abc3</name_3><name_4 name="value_4" tag="tag_4">abc4</name_4></sub_elemtnt1><sub_elemtnt2><name_0 name="value_0" tag="tag_0">efg0</name_0><name_1 name="value_1" tag="tag_1">efg1</name_1><name_2 name="value_2" tag="tag_2">efg2</name_2><name_3 name="value_3" tag="tag_3">efg3</name_3><name_4 name="value_4" tag="tag_4">efg4</name_4></sub_elemtnt2></Root>

example2.xml

<?xml version="1.0" encoding="utf-8"?>
    <Root>
        <sub_elemtnt1>
            <name_0 name="value_0" tag="tag_0">abc0</name_0>
            <name_1 name="value_1" tag="tag_1">abc1</name_1>
            <name_2 name="value_2" tag="tag_2">abc2</name_2>
            <name_3 name="value_3" tag="tag_3">abc3</name_3>
            <name_4 name="value_4" tag="tag_4">abc4</name_4>
        </sub_elemtnt1>
        <sub_elemtnt2>
            <name_0 name="value_0" tag="tag_0">efg0</name_0>
            <name_1 name="value_1" tag="tag_1">efg1</name_1>
            <name_2 name="value_2" tag="tag_2">efg2</name_2>
            <name_3 name="value_3" tag="tag_3">efg3</name_3>
            <name_4 name="value_4" tag="tag_4">efg4</name_4>
        </sub_elemtnt2>
    </Root>
根節點名字: Root
根節點下的位元組點: sub_elemtnt1
name_0 {'name': 'value_0', 'tag': 'tag_0'} abc0
name_1 {'name': 'value_1', 'tag': 'tag_1'} abc1
name_2 {'name': 'value_2', 'tag': 'tag_2'} abc2
name_3 {'name': 'value_3', 'tag': 'tag_3'} abc3
name_4 {'name': 'value_4', 'tag': 'tag_4'} abc4
根節點下的位元組點: sub_elemtnt2
name_0 {'name': 'value_0', 'tag': 'tag_0'} efg0
name_1 {'name': 'value_1', 'tag': 'tag_1'} efg1
name_2 {'name': 'value_2', 'tag': 'tag_2'} efg2
name_3 {'name': 'value_3', 'tag': 'tag_3'} efg3
name_4 {'name': 'value_4', 'tag': 'tag_4'} efg4