環境
python:3.4.4
準備xml檔案
首先新建一個xml檔案,countries.xml。內容是在python官網上看到的。
<?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>
準備python檔案
新建一個test_ET.py,用來解析xml檔案。
#!/usr/bin/python # -*- coding=utf-8 -*- import xml.etree.ElementTree as ET from xml.etree.ElementTree import Element tree = ET.parse('countries.xml') nodes = tree.findall("country") for node in nodes: #search node & attribute & text print ("*****Country*****") if node.attrib["name"]: print ("Name:",node.attrib["name"]) rank=node.find("rank") print ("Rank:",rank.text) year=node.find("year") print ("Year:",year.text) gdppc=node.find("gdppc") print ("Gdppc:",gdppc.text) neighbors=node.findall("neighbor") for neighbor in neighbors: print ("Neighbor:",neighbor.attrib["name"]) #add node rank=node.find("rank") element=Element("rank_next", {"name":"Rank","create":"20151231"}) element.text="5" rank.append(element) #delete node year=node.find("year") node.remove(year) #add node attribute node.set("force","NewForce") #update node attribute node.set("name","NewNode") #delete node attribute neighbors=node.findall("neighbor") for neighbor in neighbors: del neighbor.attrib["direction"] #add node text neighbors=node.findall("neighbor") for neighbor in neighbors: neighbor.text = "Hello,Neighbor" #update node text gdppc=node.find("gdppc") gdppc.text = "11111" #delete node text rank=node.find("rank") rank.text = "" tree.write("./out.xml", encoding="utf-8",xml_declaration=True)
執行結果
控制檯:
>python test_ET.py *****Country***** Name: Liechtenstein Rank: 1 Year: 2008 Gdppc: 141100 Neighbor: Austria Neighbor: Switzerland *****Country***** Name: Singapore Rank: 4 Year: 2011 Gdppc: 59900 Neighbor: Malaysia *****Country***** Name: Panama Rank: 68 Year: 2011 Gdppc: 13600 Neighbor: Costa Rica Neighbor: Colombia
out.xml檔案:
<?xml version='1.0' encoding='utf-8'?> <data> <country force="NewForce" name="NewNode"> <rank><rank_next create="20151231" name="Rank">5</rank_next></rank> <gdppc>11111</gdppc> <neighbor name="Austria">Hello,Neighbor</neighbor> <neighbor name="Switzerland">Hello,Neighbor</neighbor> </country> <country force="NewForce" name="NewNode"> <rank><rank_next create="20151231" name="Rank">5</rank_next></rank> <gdppc>11111</gdppc> <neighbor name="Malaysia">Hello,Neighbor</neighbor> </country> <country force="NewForce" name="NewNode"> <rank><rank_next create="20151231" name="Rank">5</rank_next></rank> <gdppc>11111</gdppc> <neighbor name="Costa Rica">Hello,Neighbor</neighbor> <neighbor name="Colombia">Hello,Neighbor</neighbor> </country> </data>
備註
具有方便友好的API。程式碼可用性好,速度快,消耗記憶體少。
最適合用來處理XML文件。
參考:https://docs.python.org/2/library/xml.etree.elementtree.html
tree = ET.parse('countries.xml')
解析countries.xml並返回一個樹。
tree.write("./out2.xml", encoding="utf-8",xml_declaration=True)
將元素樹寫入到文件,採用 “utf-8”編碼,具有xml宣告。
write(file, encoding="us-ascii", xml_declaration=None, default_namespace=None, method="xml")
Writes the element tree to a file, as XML. file is a file name, or a file object opened for writing. encoding [1] is the output encoding (default is US-ASCII). xml_declaration controls if an XML declaration should be added to the file. Use False for never, True for always, None for only if not US-ASCII or UTF-8 (default is None). default_namespace sets the default XML namespace (for “xmlns”). method is either "xml", "html" or "text" (default is "xml"). Returns an encoded string.