python的內置模塊xml模塊方法 xml解析 詳解以及使用


一、XML介紹

xml是實現不同語言或程序直接進行數據交換的協議,跟json差不多,單json使用起來更簡單,不過現在還有很多傳統公司的接口主要還是xml

xml跟html都屬於是標簽語言

我們主要學習的是ElementTree是python的XML處理模塊,它提供了一個輕量級的對象模型,在使用ElementTree模塊時,需要import xml.etree.ElementTre
ElementTree相當於整個xml節點樹,而Element表示節點數中的一個單獨節點
我們看下下面的xml文本,標簽分為2種。
1、自閉和標簽  <rank updated="yes">2</rank>
2、非閉合標簽  <neighbor direction="E" name="Austria" />
<data>
    <country name="Liechtenstein">
        <rank updated="yes">2</rank>
        <year updated="yes">2010</year>
        <gdppc>141100</gdppc>
        <neighbor direction="E" name="Austria" />
        <neighbor direction="W" name="Switzerland" />
    </country>
    <country name="Singapore">
        <rank updated="yes">5</rank>
        <year updated="yes">2013</year>
        <gdppc>59900</gdppc>
        <neighbor direction="N" name="Malaysia" />
    </country>
    <country name="Panama">
        <rank updated="yes">69</rank>
        <year updated="yes">2013</year>
        <gdppc>13600</gdppc>
        <neighbor direction="W" name="Costa Rica" />
        <neighbor direction="E" name="Colombia" />
    </country>
</data> 

二、xml.etree.ElementTree 模塊的具體應用

1、打印根標簽的名字

import xml.etree.ElementTree as ET #導入模塊,名字太長了,把這個模塊名重命名為ET
tree = ET.parse("xml_lesson.xml")#parse解析,用ET模塊下的parse這個方法把xml文件解析開,解析開拿到一個tree,tree就是一個對象
root = tree.getroot()#這個對象可以調用方法,getroot就是根的意思
print(root.tag)#root這個對象有一個屬性tag,tag的值就是根標簽的名字

C:\python35\python3.exe D:/pyproject/day21模塊/xml_test

data

什么是標簽,什么是屬性,舉個例子

<country name="Liechtenstein">

conuntry是標簽 

name="Liechtenstein" 是這個標簽的屬性

 

<neighbor direction="W" name="Costa Rica" />

neighbor是標簽

direction="W" name="Costa Rica"  這2個都是標簽的屬性

2、用for循環查看下root下面是什么東西

for n in root:
    print(n)

C:\python35\python3.exe D:/pyproject/day21模塊/xml_test

<Element 'country' at 0x0000000000E339F8>

<Element 'country' at 0x0000000000E38408>

<Element 'country' at 0x0000000000E38598>

這3個地址指向的就是對象,打印下這三個標簽的名字,用tag這個屬性就可以了

for n in root:
    print(n.tag)

C:\python35\python3.exe D:/pyproject/day21模塊/xml_test

country

country

country

在打印下country下面的標簽

for n in root:
    for i in n:
        print(i.tag)

C:\python35\python3.exe D:/pyproject/day21模塊/xml_test

rank

year

gdppc

neighbor

neighbor

rank

year

gdppc

neighbor

rank

year

gdppc

neighbor

neighbor

3、打印標簽的屬性 attrib

打印country的屬性

for n in root:
    print(n.attrib)

C:\python35\python3.exe D:/pyproject/day21模塊/xml_test

{'name': 'Liechtenstein'}

{'name': 'Singapore'}

{'name': 'Panama'}

在打印下country里面的對象的屬性

for n in root:
    for i in n:
        print(i.attrib)

C:\python35\python3.exe D:/pyproject/day21模塊/xml_test

{'updated': 'yes'}

{'updated': 'yes'}

{}

{'name': 'Austria', 'direction': 'E'}

{'name': 'Switzerland', 'direction': 'W'}

{'updated': 'yes'}

{'updated': 'yes'}

{}

{'name': 'Malaysia', 'direction': 'N'}

{'updated': 'yes'}

{'updated': 'yes'}

{}

{'name': 'Costa Rica', 'direction': 'W'}

{'name': 'Colombia', 'direction': 'E'}

4、text  標簽實際包裹的內容

for n in root:
    for i in n:
        print(i.text)

C:\python35\python3.exe D:/pyproject/day21模塊/xml_test

2

2010

141100

None

None

5

2013

59900

None

69

2013

13600

None

None 

5、iter  

想取到每個屬性中的year的text值就應該用iter方法這樣取

for n in root.iter("year"):
    print(n.tag,n.text)

C:\python35\python3.exe D:/pyproject/day21模塊/xml_test

year 2010

year 2013

year 2013

6、對xml文件數據進行修改操作

import xml.etree.ElementTree as ET #導入模塊,名字太長了,把這個模塊名重命名為ET

tree = ET.parse("xml_lesson.xml")#parse解析,用ET模塊下的parse這個方法把xml文件解析開,
                                   #解析開拿到一個tree,tree就是一個對象
root = tree.getroot()#這個對象可以調用方法,getroot就是根的意思
# print(root.tag)#root這個對象有一個屬性tag,tag的值就是根標簽的名字

for n in root.iter("year"):
    new_year=int(n.text)+1
    n.text=str(new_year)#修改year標簽的text屬性
    n.set("updated1","yes")#給year這個標簽增加一個屬性
tree.write("xml_lesson.xml")#直接把修改的寫入到文件中
<data>
    <country name="Liechtenstein">
        <rank updated="yes">2</rank>
        <year updated="yes" updated1="yes">2012</year>
        <gdppc>141100</gdppc>
        <neighbor direction="E" name="Austria" />
        <neighbor direction="W" name="Switzerland" />
    </country>
    <country name="Singapore">
        <rank updated="yes">5</rank>
        <year updated="yes" updated1="yes">2015</year>
        <gdppc>59900</gdppc>
        <neighbor direction="N" name="Malaysia" />
    </country>
    <country name="Panama">
        <rank updated="yes">69</rank>
        <year updated="yes" updated1="yes">2015</year>
        <gdppc>13600</gdppc>
        <neighbor direction="W" name="Costa Rica" />
        <neighbor direction="E" name="Colombia" />
    </country>
</data>

7、對xml文件進行刪除操作,比如刪除排名大於50的國家,需要取到每個conutry中的rank的text

import xml.etree.ElementTree as ET #導入模塊,名字太長了,把這個模塊名重命名為ET

tree = ET.parse("xml_lesson.xml")#parse解析,用ET模塊下的parse這個方法把xml文件解析開,
                                   #解析開拿到一個tree,tree就是一個對象
root = tree.getroot()#這個對象可以調用方法,getroot就是根的意思
# print(root.tag)#root這個對象有一個屬性tag,tag的值就是根標簽的名字

for n in root.findall("country"):#找到所有的country
    rank=int(n.find("rank").text)#找到所有的rank的text值
    if rank > 50:#判斷值大於50的
        root.remove(n)#就刪除country這個標簽
tree.write("xml_lesson.xml")#寫入文件

刪除之后,文件里面只有2個country了

<data>
    <country name="Liechtenstein">
        <rank updated="yes">2</rank>
        <year updated="yes" updated1="yes">2012</year>
        <gdppc>141100</gdppc>
        <neighbor direction="E" name="Austria" />
        <neighbor direction="W" name="Switzerland" />
    </country>
    <country name="Singapore">
        <rank updated="yes">5</rank>
        <year updated="yes" updated1="yes">2015</year>
        <gdppc>59900</gdppc>
        <neighbor direction="N" name="Malaysia" />
    </country>
    </data>

8、如何通過模塊創建xml文件呢

import xml.etree.ElementTree as ET #導入模塊,名字太長了,把這個模塊名重命名為ET
new_xml=ET.Element("namelist")#創建了一個根節點
#相當於創建了<namelist></namelist>
name=ET.SubElement(new_xml,"name",attrib={"enrolled":"yes"})
#創建一個子標簽name,然后增加一個屬性
age=ET.SubElement(name,"age",attrib={"checked":"no"})
sex=ET.SubElement(name,"sex")
sex.text="28"


et=ET.ElementTree(new_xml)#生成文檔對象
et.write("test.xml",encoding="utf8",xml_declaration=True)

查看下生成的text.xml這個文件內容

<namelist>

<name enrolled="yes">

<age checked="no"/>

<sex>28</sex>

</name>

</namelist> 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM