Python XML操做

XML(可擴展性標記語言)是一種很是經常使用的文件類型,主要用於存儲和傳輸數據。在編程中,對XML的操做也很是常見。html

本文根據python庫文檔中的xml.etree.ElementTree類來進行介紹XML的解析:https://docs.python.org/3.5/library/xml.etree.elementtree.html python

BTW,xml.etree.cElementTree模塊從3.3之後就被棄用了.編程

XML格式

首先,來看一下XML所包含的元素類型app

1. 標籤 <tag>學習

2. 屬性 <tag  name="attribute">spa

3. 數據 <data>1<data>code

 例如 xml段:xml

<?xml version="1.0"?>
<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>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>

 

XML操做

  • 讀取

#從變量讀取,參數爲XML段,返回的是一個根Element對象
root = ET.fromstring(country_data_as_string)

#從xml文件中讀取,用getroot獲取根節點,根節點也是Element對象
tree = ET.parse('file.xml')
root = tree.getroot()
  • 訪問

    • 訪問Element對象的標籤、屬性和值
tag = element.tag
attrib = element.attrib
value = element.text
    • 訪問子節點
#打印根節點的標籤和屬性,獲取
for child in root:
    print(child.tag, child.attrib)
  • 查找操做

    • Element元素迭代子元素:Element.iter("tag"),能夠羅列該節點所包含的全部其餘節點(element對象)
#打印根節點中全部的neighbor對象的name屬性
for neighbor in root.iter('neighbor'):
    print(neighbor.attrib['name'])
    • Element.findall("tag"):查找當前元素爲「tag」的直接子元素
#findall只能用來查找直接子元素,不能用來查找rank,neighbor等element
for country in root.findall('country'):
    rank = country.find('rank').text
    name = country.find('rank').text
    neig = country.find('neighbor').attrib
    print(rank, name,neig)
    • Element.find("tag"):查找爲tag的第一個直接子元素
#返回第一個tag爲country的element,如沒有,返回None
firstCountry = root.find("country")
print(firstCountry)
  • 建立xml文件

__author__ = 'xua'

import xml.etree.ElementTree as ET
#建立根節點
a = ET.Element("root")
#建立子節點,並添加屬性
b = ET.SubElement(a,"sub1")
b.attrib = {"name":"name attribute"}
#建立子節點,並添加數據
c = ET.SubElement(a,"sub2")
c.text = "test"

#建立elementtree對象,寫文件
tree = ET.ElementTree(a)
tree.write("test.xml")

建立的新文件內容爲:<root><sub1 name="name attribute" /><sub2>test</sub2></root>htm

  • 修改XML文件

    • ElementTree.write("xmlfile"):更新xml文件
    • Element.append():爲當前element對象添加子元素(element)
    • Element.set(key,value):爲當前element的key屬性設置value值
    • Element.remove(element):刪除爲element的節點
#讀取待修改文件
updateTree = ET.parse("test.xml")
root = updateTree.getroot()
#建立新節點並添加爲root的子節點
newEle = ET.Element("NewElement")
newEle.attrib = {"name":"NewElement","age":"20"}
newEle.text = "This is a new element"
root.append(newEle)

#修改sub1的name屬性
sub1 = root.find("sub1")
sub1.set("name","New Name")

#修改sub2的數據值
sub2 = root.find("sub2")
sub2.text = "New Value"

#寫回原文件
updateTree.write("test.xml")

更新完的文件爲:<root><sub1 name="New Name" /><sub2>New Value</sub2><NewElement age="20" name="NewElement">This is a new element</NewElement></root>對象

 

總結

 XML的操做比較常見,固然也有不少第三方的庫能夠使用,所須要作的操做無非就是經常使用的讀寫xml文件、元素節點的增刪改查,你們還能夠在python官方文檔上學習更多的操做。

https://docs.python.org/3.5/library/xml.etree.elementtree.html 

相關文章
相關標籤/搜索