1. 程式人生 > 程式設計 >Python如何使用ElementTree解析xml

Python如何使用ElementTree解析xml

以country.xml為例,內容如下:

<?xml version="1.0"?>
<data>
  <country name="Liechtenstein">
    <rank updated="yes">2</rank>
    <year>2008</year>
    <gdppc>141100</gdppc>
    <neighbor name="Austria" direction="E"/>
    <neighbor name="Switzerland" direction="W"/>
  </country>
  <country name="Singapore">
    <rank updated="yes">5</rank>
    <year>2011</year>
    <gdppc>59900</gdppc>
    <neighbor name="Malaysia" direction="N"/>
  </country>
  <country name="Panama">
    <rank updated="yes">69</rank>
    <year>2011</year>
    <gdppc>13600</gdppc>
    <neighbor name="Costa Rica" direction="W"/>
    <neighbor name="Colombia" direction="E"/>
  </country>
</data>

1.解析

1)呼叫parse()方法,返回解析樹

try:
  import xml.etree.cElementTree as ET
except ImportError:
  import xml.etree.ElementTree as ET

tree = ET.parse("country.xml") # <class 'xml.etree.ElementTree.ElementTree'>
root = tree.getroot()      # 獲取根節點 <Element 'data' at 0x02BF6A80>

2)呼叫from_string(),返回解析樹的根元素

import xml.etree.ElementTree as ET
data = open("country.xml").read()
root = ET.fromstring(data) # <Element 'data' at 0x036168A0>

3)呼叫ElementTree類ElementTree(self,element=None,file=None) # 這裡的element作為根節點

import xml.etree.ElementTree as ET
tree = ET.ElementTree(file="country.xml") # <xml.etree.ElementTree.ElementTree object at 0x03031390>

root = tree.getroot() # <Element 'data' at 0x030EA600>

1)簡單遍歷

import xml.etree.ElementTree as ET

tree = ET.parse("country.xml")
root = tree.getroot()
print(root.tag,":",root.attrib) # 列印根元素的tag和屬性
# 遍歷xml文件的第二層
for child in root:
  # 第二層節點的標籤名稱和屬性
  print(child.tag,child.attrib) 
  # 遍歷xml文件的第三層
  for children in child:
    # 第三層節點的標籤名稱和屬性
    print(children.tag,children.attrib)

可以通過下標的方式直接訪問節點

# 訪問根節點下第一個country的第二個節點year,獲取對應的文字
year = root[0][1].text # 2008

2)ElementTree提供的方法

find(match) # 查詢第一個匹配的子元素, match可以時tag或是xpaht路徑
findall(match) # 返回所有匹配的子元素列表
findtext(match,default=None) #
iter(tag=None) # 以當前元素為根節點 建立樹迭代器,如果tag不為None,則以tag進行過濾
iterfind(match) #

例子:

# 過濾出所有neighbor標籤
for neighbor in root.iter("neighbor"):
print(neighbor.tag,neighbor.attrib)

# 遍歷所有的counry標籤
for country in root.findall("country"):
# 查詢country標籤下的第一個rank標籤
rank = country.find("rank").text
# 獲取country標籤的name屬性
name = country.get("name")
print(name,rank)

1) 屬性相關

# 將所有的rank值加1,並新增屬性updated為yes
for rank in root.iter("rank"):
  new_rank = int(rank.text) + 1
  rank.text = str(new_rank) # 必須將int轉為str
  rank.set("updated","yes") # 新增屬性

# 再終端顯示整個xml
ET.dump(root)
# 注意 修改的內容存在記憶體中 尚未儲存到檔案中
# 儲存修改後的內容
tree.write("output.xml")
import xml.etree.ElementTree as ET

tree = ET.parse("output.xml")
root = tree.getroot()

for rank in root.iter("rank"):
  # attrib為屬性字典
  # 刪除對應的屬性updated
  del rank.attrib['updated'] 

ET.dump(root)

小結: 關於classxml.etree.ElementTree.Element 屬性相關

  • attrib    為包含元素屬性的字典
  • keys() 返回元素屬性名稱列表
  • items() 返回(name,value)列表
  • get(key,default=None) 獲取屬性
  • set(key,value) # 跟新/新增 屬性
  • del xxx.attrib[key] # 刪除對應的屬性

2) 節點/元素 相關

刪除子元素remove()

import xml.etree.ElementTree as ET

tree = ET.parse("country.xml")
root = tree.getroot()

# 刪除rank大於50的國家
for country in root.iter("country"):
  rank = int(country.find("rank").text)
  if rank > 50:
    # remove()方法 刪除子元素
    root.remove(country)

ET.dump(root)

新增子元素

程式碼:

import xml.etree.ElementTree as ET

tree = ET.parse("country.xml")
root = tree.getroot()

country = root[0]
last_ele = country[len(list(country))-1]
last_ele.tail = '\n\t\t'
# 建立新的元素,tag為test_append
elem1 = ET.Element("test_append")
elem1.text = "elem 1"
# elem.tail = '\n\t'
country.append(elem1)

# SubElement() 其實內部呼叫的時append()
elem2 = ET.SubElement(country,"test_subelement")
elem2.text = "elem 2"

# extend()
elem3 = ET.Element("test_extend")
elem3.text = "elem 3"
elem4 = ET.Element("test_extend")
elem4.text = "elem 4"
country.extend([elem3,elem4])

# insert()
elem5 = ET.Element("test_insert")
elem5.text = "elem 5"
country.insert(5,elem5)

ET.dump(country)

效果:

Python如何使用ElementTree解析xml

新增子元素方法總結:

  • append(subelement)
  • extend(subelements)
  • insert(index,element)

4.建立xml文件

想建立root Element,然後建立SubElement,最後將root element傳入ElementTree(element),建立tree,呼叫tree.write()方法寫入檔案

對於建立元素的3個方法: 使用ET.Element、Element物件的makeelement()方法以及ET.SubElement

import xml.etree.ElementTree as ET


def subElement(root,tag,text):
  ele = ET.SubElement(root,tag)
  ele.text = text
  ele.tail = '\n'


root = ET.Element("note")

to = root.makeelement("to",{})
to.text = "peter"
to.tail = '\n'
root.append(to)

subElement(root,"from","marry")
subElement(root,"heading","Reminder")
subElement(root,"body","Don't forget the meeting!")

tree = ET.ElementTree(root)
tree.write("note.xml",encoding="utf-8",xml_declaration=True)

效果:

Python如何使用ElementTree解析xml

由於原生儲存的XML時預設無縮排,如果想要設定縮排的話, 需要修改儲存方式

程式碼:

import xml.etree.ElementTree as ET
from xml.dom import minidom


def subElement(root,tag)
  ele.text = text


def saveXML(root,filename,indent="\t",newl="\n",encoding="utf-8"):
  rawText = ET.tostring(root)
  dom = minidom.parseString(rawText)
  with open(filename,'w') as f:
    dom.writexml(f,"",indent,newl,encoding)
root = ET.Element("note")

to = root.makeelement("to",{})
to.text = "peter"
root.append(to)

subElement(root,"Don't forget the meeting!")

# 儲存xml檔案
saveXML(root,"note.xml")

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。