首页 技术 正文
技术 2022年11月17日
0 收藏 431 点赞 3,787 浏览 5147 个字

一、XML介绍

xml是实现不同语言或程序直接进行数据交换的协议,跟json差不多,单json使用起来更简单,不过现在还有很多传统公司的接口主要还是xml

xml跟html都属于是标签语言

我们主要学习的是ElementTree是python的XML处理模块,它提供了一个轻量级的对象模型,在使用ElementTree模块时,需要import xml.etree.ElementTre
ElementTree相当于整个xml节点树,而Element表示节点数中的一个单独节点
我们看下下面的xml文本,标签分为2种。
1、自闭和标签  <rank updated="yes">2</rank>
2、非闭合标签  <neighbor direction="E" name="Austria" />
<data>
<country name="Liechtenstein">
<rank updated="yes">2</rank>
<year updated="yes">2010</year>
<gdppc>141100</gdppc>
<neighbor direction="E" name="Austria" />
<neighbor direction="W" name="Switzerland" />
</country>
<country name="Singapore">
<rank updated="yes">5</rank>
<year updated="yes">2013</year>
<gdppc>59900</gdppc>
<neighbor direction="N" name="Malaysia" />
</country>
<country name="Panama">
<rank updated="yes">69</rank>
<year updated="yes">2013</year>
<gdppc>13600</gdppc>
<neighbor direction="W" name="Costa Rica" />
<neighbor direction="E" name="Colombia" />
</country>
</data> 

二、xml.etree.ElementTree 模块的具体应用

1、打印根标签的名字

import xml.etree.ElementTree as ET #导入模块,名字太长了,把这个模块名重命名为ET
tree = ET.parse("xml_lesson.xml")#parse解析,用ET模块下的parse这个方法把xml文件解析开,解析开拿到一个tree,tree就是一个对象
root = tree.getroot()#这个对象可以调用方法,getroot就是根的意思
print(root.tag)#root这个对象有一个属性tag,tag的值就是根标签的名字C:\python35\python3.exe D:/pyproject/day21模块/xml_testdata

什么是标签,什么是属性,举个例子

<country name=”Liechtenstein”>

conuntry是标签

name=”Liechtenstein” 是这个标签的属性

<neighbor direction=”W” name=”Costa Rica” />

neighbor是标签

direction=”W” name=”Costa Rica”  这2个都是标签的属性

2、用for循环查看下root下面是什么东西

for n in root:
print(n)C:\python35\python3.exe D:/pyproject/day21模块/xml_test<Element 'country' at 0x0000000000E339F8><Element 'country' at 0x0000000000E38408><Element 'country' at 0x0000000000E38598>

这3个地址指向的就是对象,打印下这三个标签的名字,用tag这个属性就可以了

for n in root:
print(n.tag)C:\python35\python3.exe D:/pyproject/day21模块/xml_testcountrycountrycountry

在打印下country下面的标签

for n in root:
for i in n:
print(i.tag)C:\python35\python3.exe D:/pyproject/day21模块/xml_testrankyeargdppcneighborneighborrankyeargdppcneighborrankyeargdppcneighborneighbor

3、打印标签的属性 attrib

打印country的属性

for n in root:
print(n.attrib)C:\python35\python3.exe D:/pyproject/day21模块/xml_test{'name': 'Liechtenstein'}{'name': 'Singapore'}{'name': 'Panama'}

在打印下country里面的对象的属性

for n in root:
for i in n:
print(i.attrib)C:\python35\python3.exe D:/pyproject/day21模块/xml_test{'updated': 'yes'}{'updated': 'yes'}{}{'name': 'Austria', 'direction': 'E'}{'name': 'Switzerland', 'direction': 'W'}{'updated': 'yes'}{'updated': 'yes'}{}{'name': 'Malaysia', 'direction': 'N'}{'updated': 'yes'}{'updated': 'yes'}{}{'name': 'Costa Rica', 'direction': 'W'}{'name': 'Colombia', 'direction': 'E'}

4、text  标签实际包裹的内容

for n in root:
for i in n:
print(i.text)C:\python35\python3.exe D:/pyproject/day21模块/xml_test22010141100NoneNone5201359900None69201313600NoneNone 

5、iter

想取到每个属性中的year的text值就应该用iter方法这样取

for n in root.iter("year"):
print(n.tag,n.text)C:\python35\python3.exe D:/pyproject/day21模块/xml_testyear 2010year 2013year 2013

6、对xml文件数据进行修改操作

import xml.etree.ElementTree as ET #导入模块,名字太长了,把这个模块名重命名为ETtree = ET.parse("xml_lesson.xml")#parse解析,用ET模块下的parse这个方法把xml文件解析开,
#解析开拿到一个tree,tree就是一个对象
root = tree.getroot()#这个对象可以调用方法,getroot就是根的意思
# print(root.tag)#root这个对象有一个属性tag,tag的值就是根标签的名字for n in root.iter("year"):
new_year=int(n.text)+1
n.text=str(new_year)#修改year标签的text属性
n.set("updated1","yes")#给year这个标签增加一个属性
tree.write("xml_lesson.xml")#直接把修改的写入到文件中
<data>
<country name="Liechtenstein">
<rank updated="yes">2</rank>
<year updated="yes" updated1="yes">2012</year>
<gdppc>141100</gdppc>
<neighbor direction="E" name="Austria" />
<neighbor direction="W" name="Switzerland" />
</country>
<country name="Singapore">
<rank updated="yes">5</rank>
<year updated="yes" updated1="yes">2015</year>
<gdppc>59900</gdppc>
<neighbor direction="N" name="Malaysia" />
</country>
<country name="Panama">
<rank updated="yes">69</rank>
<year updated="yes" updated1="yes">2015</year>
<gdppc>13600</gdppc>
<neighbor direction="W" name="Costa Rica" />
<neighbor direction="E" name="Colombia" />
</country>
</data>

7、对xml文件进行删除操作,比如删除排名大于50的国家,需要取到每个conutry中的rank的text

import xml.etree.ElementTree as ET #导入模块,名字太长了,把这个模块名重命名为ETtree = ET.parse("xml_lesson.xml")#parse解析,用ET模块下的parse这个方法把xml文件解析开,
#解析开拿到一个tree,tree就是一个对象
root = tree.getroot()#这个对象可以调用方法,getroot就是根的意思
# print(root.tag)#root这个对象有一个属性tag,tag的值就是根标签的名字for n in root.findall("country"):#找到所有的country
rank=int(n.find("rank").text)#找到所有的rank的text值
if rank > 50:#判断值大于50的
root.remove(n)#就删除country这个标签
tree.write("xml_lesson.xml")#写入文件

删除之后,文件里面只有2个country了

<data>
<country name="Liechtenstein">
<rank updated="yes">2</rank>
<year updated="yes" updated1="yes">2012</year>
<gdppc>141100</gdppc>
<neighbor direction="E" name="Austria" />
<neighbor direction="W" name="Switzerland" />
</country>
<country name="Singapore">
<rank updated="yes">5</rank>
<year updated="yes" updated1="yes">2015</year>
<gdppc>59900</gdppc>
<neighbor direction="N" name="Malaysia" />
</country>
</data>

8、如何通过模块创建xml文件呢

import xml.etree.ElementTree as ET #导入模块,名字太长了,把这个模块名重命名为ET
new_xml=ET.Element("namelist")#创建了一个根节点
#相当于创建了<namelist></namelist>
name=ET.SubElement(new_xml,"name",attrib={"enrolled":"yes"})
#创建一个子标签name,然后增加一个属性
age=ET.SubElement(name,"age",attrib={"checked":"no"})
sex=ET.SubElement(name,"sex")
sex.text=""et=ET.ElementTree(new_xml)#生成文档对象
et.write("test.xml",encoding="utf8",xml_declaration=True)

查看下生成的text.xml这个文件内容

<namelist><name enrolled="yes"><age checked="no"/><sex>28</sex></name></namelist> 
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,078
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,553
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,402
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,177
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,814
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898