Python xml.etree.ElementTree解析XML文件实例演示(十六02)

(一)country_data.xml
<?xml version="1.0"?>
<data name="Kaina" age="18">
    <country name="列支敦斯登">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="澳大利亚" direction="东部"/>
        <neighbor name="新西兰" direction="西部"/>
    </country>
    <country name="新加坡">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="马来西亚" direction="北部"/>
    </country>
    <country name=" 巴拿马">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="哥斯达黎加" direction="西部"/>
        <neighbor name=" 哥伦比亚" direction="东部"/>
    </country>
</data>
1.对XML文档进行读取操作
import xml.etree.ElementTree as ET
#1.解析xml文件,返回ElementTree对象
tree = ET.parse('country_data.xml')
#2.获得根节点
root = tree.getroot()
#3.打印根节点标签名
print("coutry_data.xml的根节点:"+root.tag)
#4.打印出根节点的属性和属性值
print("根节点标签里的属性和属性值:"+str(root.attrib))
#5.通过遍历获取孩子节点的标签、属性和属性值
for child in root:
    print(child.tag, child.attrib)
#6.获取country标签下的子标签的内容
print("排名:"+root[0][0].text,"国内生产总值:"+root[0][2].text,)
#7.把所有neighbor标签找出来,并打印出标签的属性和属性值。
for neighbor in root.iter('neighbor'):
    print(neighbor.attrib)
#8.使用findall()方法把满足条件的标签找出来迭代。
for country in root.findall('country'):
    rank = country.find('rank').text
    name = country.get('name')
    print(name,rank)
输出结果:
---------------------------------------------
coutry_data.xml的根节点:data
根节点标签里的属性和属性值:{'name': 'Kaina', 'age': '18'}
---------------------------------------------
country {'name': '列支敦斯登'}
country {'name': '新加坡'}
country {'name': ' 巴拿马'}
排名:1 国内生产总值:141100
---------------------------------------------
{'name': '澳大利亚', 'direction': '东部'}
{'name': '新西兰', 'direction': '西部'}
{'name': '马来西亚', 'direction': '北部'}
{'name': '哥斯达黎加', 'direction': '西部'}
{'name': ' 哥伦比亚', 'direction': '东部'}
---------------------------------------------
列支敦斯登 1
新加坡 4
 巴拿马 68
---------------------------------------------
2.对XML文件进行修改操作
import xml.etree.ElementTree as ET
#1.解析xml文件,返回ElementTree对象
tree = ET.parse('country_data.xml')
#2.获得根节点
root = tree.getroot()
#3.遍历修改标签(添加属性和属性值、修改属性值、删除标签)
for rank in root.iter("rank"):
    new_rank=int(rank.text)+1
    rank.text=str(new_rank)
    rank.set("updated","yes")
#4.write()的作用:创建文件,并把xml写入新的文件
#5.指定写入内容的编码
tree.write("output.xml",encoding="utf-8")

修改后保存在output.xml文件的数据:

<data age="18" name="Kaina">
    <country name="列支敦斯登">
        <rank updated="yes">2</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor direction="东部" name="澳大利亚" />
        <neighbor direction="西部" name="新西兰" />
    </country>
    <country name="新加坡">
        <rank updated="yes">5</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor direction="北部" name="马来西亚" />
    </country>
    <country name=" 巴拿马">
        <rank updated="yes">69</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor direction="西部" name="哥斯达黎加" />
        <neighbor direction="东部" name=" 哥伦比亚" />
    </country>
</data>
3.对XML文件进行删除操作
import xml.etree.ElementTree as ET
#1.解析xml文件,返回ElementTree对象
tree = ET.parse('country_data.xml')
#2.获得根节点
root = tree.getroot()
#3.通过遍历获得满足条件的元素,并使用remove()指定删除
for country in root.findall('country'):
    rank=int(country.find("rank").text)
    if rank>50:
        root.remove(country)
#4.删除后再把数据保存到output.xml文件中
tree.write("output.xml",encoding="utf-8")

删除后保存在output.xml文件的数据:

<data age="18" name="Kaina">
    <country name="列支敦斯登">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor direction="东部" name="澳大利亚" />
        <neighbor direction="西部" name="新西兰" />
    </country>
    <country name="新加坡">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor direction="北部" name="马来西亚" />
    </country>
    </data>
    原文作者:凯耐
    原文地址: https://blog.csdn.net/weixin_36279318/article/details/79180721
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞