xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但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>

xml协议在各个语言里的都 是支持的,在python中可以用以下模块操作xml 

import xml.etree.ElementTree as ET

tree = ET.parse("xml_test.xml") #open
root = tree.getroot() #f.seek(0)
print(root.tag)

#遍历xml文档
for child in root: #data 下一层的关键词country
    print(child.tag, child.attrib)
    print('----i start----')
    for i in child:
        print(i.tag,i.text)
    print('----i stop----')

#只遍历year 节点
print('----year----')
for node in root.iter('year'):
    print(node.tag,node.text)

      运行结果

data
country {'name': 'Liechtenstein'}
----i start----
rank 2
year 2008
gdppc 141100
neighbor None
neighbor None
----i stop----
country {'name': 'Singapore'}
----i start----
rank 5
year 2011
gdppc 59900
neighbor None
----i stop----
country {'name': 'Panama'}
----i start----
rank 69
year 2011
gdppc 13600
neighbor None
neighbor None
----i stop----
----year----
year 2008
year 2011
year 2011

修改和删除xml文档

import xml.etree.ElementTree as ET

tree = ET.parse("xml_test.xml") #open
root = tree.getroot() #f.seek(0)

#修改
for node in root.iter('year'): #只从下一层里面找
    new_year = int(node.text) + 1
    node.text = str(new_year)
    node.set("attr_test","yes") #设置属性

tree.write("xmltest.xml")


#删除node
for country in root.findall('country'): #findall 找到所有的 country
   rank = int(country.find('rank').text)
   if rank > 50:
     root.remove(country)

tree.write('output.xml')

自己创建xml文件

import xml.etree.ElementTree as ET

root = ET.Element("namelist") #root

name = ET.SubElement(root,"name",attrib={"enrolled":"yes"})
age = ET.SubElement(name,"age",attrib={"checked":"no"})
sex = ET.SubElement(name,"sex")
sex.text = '33'

name2 = ET.SubElement(root,"name",attrib={"enrolled":"no"})
age = ET.SubElement(name2,"age")
age.text = '19'

et = ET.ElementTree(root) #生成文档对象
et.write("build_out.xml", encoding="utf-8", xml_declaration=True)

ET.dump(root) #打印生成的格式