首页 » 编程 » Python-4.常用模块 » 正文

xml 模块

xml是时间不同语言或程序之间进行数据交换的协议,更json差不多,但是json使用起来更简单,不过xml诞生比较早,在json之前一般都用的xml,所以现在依然有些系统在用着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("xmltest.xml")
root = tree.getroot()
print("顶级节点:", root.tag)  # 打印出根节点的标签名为 data

for child in root:
    print("-----子节点:", child.tag, child.attrib)  # 获取到下一级子节点的标签名 和 属性, 如 country {'name': 'Liechtenstein'}

    # 再进行一次遍历
    for i in child:
        print(i.tag, i.text)  # 获取到再下一级的子节点的标签名 和 具体的值,如 rank 2

运行结果:

顶级节点: data
-----子节点: country {'name': 'Liechtenstein'}
rank 2
year 2008
gdppc 141100
neighbor None
neighbor None
-----子节点: country {'name': 'Singapore'}
rank 5
year 2011
gdppc 59900
neighbor None
-----子节点: country {'name': 'Panama'}
rank 69
year 2011
gdppc 13600
neighbor None
neighbor None

 

只遍历year节点:

import xml.etree.ElementTree as ET

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

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

输出:

year 2008
year 2011
year 2011

 

修改和删除文档内容:

import xml.etree.ElementTree as ET

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

# 修改 ,给年+1
for node in root.iter("year"):
    new_text = int(node.text) + 1
    node.text = str(new_text)
    node.set("updated", "yes")  # 给year添加一个属性  updated
tree.write("xmltest.xml")

# 删除
for country in root.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

new_xml = ET.Element("namelist")  # 顶节点为 namelist
name = ET.SubElement(new_xml, "name", attrib={"enrolled": "yes"})  # 新建name节点,放到new_xml节点下,并声明属性 enrolled="yes"
age = ET.SubElement(name, "age", attrib={"checked": "no"})  # 新建 age 节点,放到 name节点下,并声明属性 checked="no"
sex = ET.SubElement(name, "sex")  # 声明sex节点,放到name节点下
sex.text = "33"  # 声明sex节点,值为33

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

et = ET.ElementTree(new_xml)  # 生成文档对象
et.write("test.xml", encoding="utf-8", xml_declaration=True)  # 写入文件,xml_declaration=True表示写入xml版本信息
ET.dump(new_xml)  # 打印生成的格式

最终生成了文件 test.xml,内容为:

<?xml version='1.0' encoding='utf-8'?>
<namelist><name enrolled="yes"><age checked="no" /><sex>33</sex></name><name enrolled="no"><age>19</age></name></namelist>

 

 

发表评论

验证码加载中....