Python XML模块
一、定义:
xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单。
xml就是实现解析xml文件的模块
二、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>
三、代码实例
import xml.etree.ElementTree as ET tree = ET.parse("xmltest.xml") root = tree.getroot() print(root.tag) #遍历xml文档 for child in root: print('========>',child.tag,child.attrib,child.attrib['name']) for i in child: print(i.tag,i.attrib,i.text) #只遍历year 节点 for node in root.iter('year'): print(node.tag,node.text) #--------------------------------------- import xml.etree.ElementTree as ET tree = ET.parse("xmltest.xml") root = tree.getroot() #修改 for node in root.iter('year'): new_year=int(node.text)+1 node.text=str(new_year) node.set('updated','yes') node.set('version','1.0') tree.write('test.xml') #删除node for country in root.findall('country'): rank = int(country.find('rank').text) if rank > 50: root.remove(country) tree.write('output.xml')
#在country内添加(append)节点year2 import xml.etree.ElementTree as ET tree = ET.parse("a.xml") root=tree.getroot() for country in root.findall('country'): for year in country.findall('year'): if int(year.text) > 2000: year2=ET.Element('year2') year2.text='新年' year2.attrib={'update':'yes'} country.append(year2) #往country节点下添加子节点 tree.write('a.xml.swap')
import xml.etree.ElementTree as ET tree=ET.parse('a.xml') root=tree.getroot() for child in root: print('====>',child.tag) for i in child: print(i.tag,i.attrib,i.text) #查找element元素的三种方式 years=root.iter('year') #扫描整个xml文档树,找到所有 for i in years: print(i) res1=root.find('country') #谁来调,就从谁下一层开始找,只找一个 print(res1) res2=root.findall('country') #谁来调,就从谁下一层开始找,只找一个 print(res2) #修改 years=root.iter('year') #扫描整个xml文档树,找到所有 for year in years: year.text=str(int(year.text)+1) year.set('updated','yes') year.set('version','1.0') tree.write('a.xml') #删除 for county in root.iter('country'): # print(county.tag) rank=county.find('rank') if int(rank.text) > 10: county.remove(rank) tree.write('a.xml') #增加节点 for county in root.iter('country'): e=ET.Element('egon') e.text='hello' e.attrib={'age':'18'} county.append(e) tree.write('a.xml')