#xml是實現不同語言或程序之間交換的協議
# import xml.etree.ElementTree as ET
#
# tree=ET.parse("fee")#parse是解析fee。xml文件
# root=tree.getroot()#拿到根節點<data></data>
# print(root.tag)#輸出確認一下
#遍歷xml文檔
# for i in root:
# # print(i.tag)#打印出<data></data>下標簽名字《contry》
# # print(i.attrib)#打印變遷的屬性值即name='dsf'
# for j in i:#找下一級的標簽名
# # print(j.tag)
# #print(j.attrib)#子標簽的屬性值{'updated': 'yes'} 源代碼<rank updated="yes">2</rank>
# print(j.text)#找到標簽內容
# for child in root:
# print(child.tag,child.attrib)
# for i in child:
# print(i.tag,i.text)
# #只遍歷year節點
# for node in root.iter('year'):#從根country拿year拿的是所有的year標簽以node為名
# print(node.tag,node.text)#標簽名以及內容
import xml.etree.ElementTree as ET#重點
res=ET.Element("namelist")#創建《name》《/name》
age=ET.SubElement(res,'name',attrib={'color':'red'})
# <namelist>
# <name color='red'></name>
# </namelist>
et=ET.ElementTree(res)#生成文檔對象,背下來
et.write('abc.xml',encoding='utf8',xml_declaration=True)#寫入,declaration表示xml聲明
tree=ET.parse("fee.xml")#parse是解析fee。xml文件tree相當於是f
root=tree.getroot()#拿到根節點<data></data>
# #修改
# for node in root.iter('year'):#拿到year標簽
# newyear=int(node.text)+1#內容值+1變成2009
# node.text=str(newyear)#轉換成字符串賦給節點
# node.set('updated','no')#為標簽設置屬性updated=’no‘
# tree.write('fee')#昨晚需要的操作之后需要寫入文件,tree是前面寫的相當於是文件操作的f
#刪除
# for node in root.iter('contry'):#重點是root.iter("")這個函數的使用
# rank=int(node.find('rank').text)#node.find("")學會使用find函數獲取rank標簽的文本內容
# if rank>5:
# root.remove(node)
# tree.write("fee")#如果寫fee.xml那么就是新建一個fee.xml.xml的文件,是新建文件寫入,所以不要加xml
新建new,選擇file,然后選擇xml
<data>
<contry name="lihai">
<rank updated="yes">2</rank>
<year updated="no">2009</year>
<gdppc>4561</gdppc>
<neighbour direction="e" name="sfa" />
<neighbour direction="g" name="tghh" />
</contry>
</data>