Python对XML文件实现增删改查操作
作者:Septieme 发布时间:2023-11-19 20:42:03
PYTHON 操作 XML
读取XML文件
关于XML的介绍
<data> 与 </data> 是一对标签的开始与结束
<property … /> 也是一个正确的标签,以 /> 结尾,是在标签没有嵌套内容时的简写形式
name=“cat”,name是<data>标签的一个属性,cat是name属性的值
description here …是<data>标签的内容,这里是一段文本。当然也可以是xml的嵌套
<data name="cat" num="10"> description here ... </data>
<property value="node" />
<country name="china">
<province name="beijing">
<school name="the sunshine school" />
</province>
</country>
准备一个demo.xml文件
<data>
<teacher name="Albert">
<birthday>1980</birthday>
<gender>male</gender>
<subject>Math</subject>
</teacher>
<student name="Becky">
<birthday>2000</birthday>
<gender>female</gender>
<hobbies>
<hobby>skating</hobby>
<hobby>rocks</hobby>
</hobbies>
<exam absence="no">
<math>90</math>
<english>90</english>
<music>95</music>
</exam>
</student>
<student name="Cindy">
<birthday>2001</birthday>
<gender>female</gender>
<hobbies>
<hobby>reading</hobby>
<hobby>guitar</hobby>
</hobbies>
<exam absence="yes">
</exam>
</student>
<student name="Duke">
<birthday>2000</birthday>
<gender>male</gender>
<hobbies>
<hobby>football</hobby>
<hobby>surfing</hobby>
</hobbies>
<exam absence="no">
<math>100</math>
<english>80</english>
<music>92</music>
</exam>
</student>
</data>
读取xml文件内容
# Read the .xml file
tree = ET.parse("demo.xml")
root = tree.getroot()
print(root)
结果
<Element 'data' at 0x102d80cf8>
遍历XML元素
for … in … 可以遍历当前元素的所有直接子节点
for n in root:
# items() returns all <key, value> pairs of the tag
print(n, n.tag , n.attrib, n.items())
结果
(<Element 'teacher' at 0x1048b9e48>, 'teacher', {'name': 'Albert'}, [('name', 'Albert')])
(<Element 'student' at 0x1048bf0f0>, 'student', {'name': 'Becky'}, [('name', 'Becky')])
(<Element 'student' at 0x1048bf3c8>, 'student', {'name': 'Cindy'}, [('name', 'Cindy')])
(<Element 'student' at 0x1048bf5f8>, 'student', {'name': 'Duke'}, [('name', 'Duke')])
想要迭代遍历当前元素的所有子节点(包括子孙节点)
for n in root.iter():
print(n, n.tag)
结果
(<Element 'data' at 0x1052f0cf8>, 'data')
(<Element 'teacher' at 0x1052f0e48>, 'teacher')
(<Element 'birthday' at 0x1052f0d30>, 'birthday')
(<Element 'gender' at 0x1052f6080>, 'gender')
(<Element 'subject' at 0x1052f60b8>, 'subject')
(<Element 'student' at 0x1052f60f0>, 'student')
(<Element 'birthday' at 0x1052f6048>, 'birthday')
(<Element 'gender' at 0x1052f6128>, 'gender')
(<Element 'hobbies' at 0x1052f6198>, 'hobbies')
(<Element 'hobby' at 0x1052f6208>, 'hobby')
(<Element 'hobby' at 0x1052f6240>, 'hobby')
(<Element 'exam' at 0x1052f62b0>, 'exam')
(<Element 'math' at 0x1052f6320>, 'math')
(<Element 'english' at 0x1052f6390>, 'english')
(<Element 'music' at 0x1052f6400>, 'music')
(<Element 'student' at 0x1052f63c8>, 'student')
(<Element 'birthday' at 0x1052f6438>, 'birthday')
(<Element 'gender' at 0x1052f6470>, 'gender')
(<Element 'hobbies' at 0x1052f64a8>, 'hobbies')
(<Element 'hobby' at 0x1052f6518>, 'hobby')
(<Element 'hobby' at 0x1052f6588>, 'hobby')
(<Element 'exam' at 0x1052f65c0>, 'exam')
(<Element 'student' at 0x1052f65f8>, 'student')
(<Element 'birthday' at 0x1052f6630>, 'birthday')
(<Element 'gender' at 0x1052f6668>, 'gender')
(<Element 'hobbies' at 0x1052f66a0>, 'hobbies')
(<Element 'hobby' at 0x1052f6710>, 'hobby')
(<Element 'hobby' at 0x1052f6780>, 'hobby')
(<Element 'exam' at 0x1052f67b8>, 'exam')
(<Element 'math' at 0x1052f6828>, 'math')
(<Element 'english' at 0x1052f6898>, 'english')
(<Element 'music' at 0x1052f6908>, 'music')
想要选择性地迭代直接子节点
for n in root.iter('teacher'):
print(n, n.tag)
(<Element 'teacher' at 0x100f29e48>, 'teacher')
查找XML元素
find与findall查找xml元素
# find the first element
print(root.find('student'))
# find all elements
print(root.findall('student'))
<Element 'student' at 0x1034300f0>
[<Element 'student' at 0x1034300f0>, <Element 'student' at 0x1034303c8>, <Element 'student' at 0x1034305f8>]
demo
for n in root:
if n.tag == 'student' and n.get('name') == 'Becky':
exam_node = n.find('exam')
for subject in exam_node:
print(subject.tag + " " + subject.text)
结果
math 90
english 90
music 95
添加XML元素
p = ET.Element(tag_name)
demo
for n in root:
if n.tag == 'student' and n.get('name') == 'Cindy':
exam_node = n.find('exam')
exam_node.set("absence", "no")
for subject in ['math', 'music']:
p = ET.Element(subject)
p.text = '90'
exam_node.append(p)
if os.path.exists('new.xml'):
os.remove('new.xml')
tree.write('new.xml', encoding='utf-8', xml_declaration=True)
结果
<student name="Cindy">
<birthday>2001</birthday>
<gender>female</gender>
<hobbies>
<hobby>reading</hobby>
<hobby>guitar</hobby>
</hobbies>
<exam absence="no">
<math>90</math><music>90</music></exam>
</student>
修改XML元素
demo
for n in root:
if n.tag == 'student' and n.get('name') == 'Cindy':
exam_node = n.find('exam')
exam_node.set("absence", "no")
exam_node.set("date", "2022-11-11")
for subject in ['math', 'music']:
p = ET.Element(subject)
p.text = '90'
exam_node.append(p)
hobbies_node = n.find('hobbies').findall("hobby")
hobbies_node[0].text = 'piano'
p = ET.Element("hobby")
p.set("old_hobby", 'yes')
p.text = 'reading'
n.find('hobbies').remove(hobbies_node[1])
n.find('hobbies').append(p)
结果
<student name="Cindy">
<birthday>2001</birthday>
<gender>female</gender>
<hobbies>
<hobby>piano</hobby>
<hobby old_hobby="yes">reading</hobby></hobbies>
<exam absence="no" date="2022-11-11">
<math>90</math><music>90</music></exam>
</student>
来源:https://blog.csdn.net/qq_52883908/article/details/127771369
猜你喜欢
- 很常见的一个图片轮播Flash,使用之后发现在IE下按F5刷新之后Flash区域就变成一大块背景色,内容轮播出不来了。有趣的是右键点击Fla
- Oracle不像SQLServer那样在存储过程中用Select就可以返回结果集,而是通过Out型的参数进行结果集返回的。实际上是利用REF
- Oracle客户端NLS_LANG设置 OracleWindowsMicrosoftUnixBash 1. NLS_LANG 参数组成 NL
- xml(可扩展标记语言)看起来可能像某种w3c标准——现在没有什么实际影响,即使以后能派上用场,也是很久以后的事。但实际上,它现在已经得到了
- asp使用fso对象遍历目录及目录下的文件代码:<%@ Language=VBScript %><%&
- <html><body><table border=1> <? $id=@mssql_connec
- 首先,与其他语言不同,JS的效率很大程度是取决于JS engine的效率。除了引擎实现的优劣外,引擎自己也会为一些特殊的代码模式采取一些优化
- asp自定义错误显示方法:<html><head><meta http-equiv="Co
- 我们可以先建立一个包含文件名,文件标题的待检索文件的数据库,然后,用ADO方式来访问它,并建立记录集对象。具体代码和说明见下:
- 背景在小站点上,直接用git来部署php代码相当方便,你的远程站点以及本地版本库都有一个版本控制,追踪问题或者回滚是很轻松的事情。因为在小公
- MySQL存储过程SAVEPOINT ROLLBACK to示例如下:DELIMITER $$DROP PROCEDURE IF EXIST
- 数据字典是Oracle存放有关数据库信息的地方,其用途是用来描述数据的。比如一个表的创建者信息,创建时间信息,所属表空间信息,用户访问权限信
- 一张表(ColumnTable)的结构如下图所示当前需要实现的功能:通过Number的值为67来获取当前的节点ID、父节点ID递归实现SQL
- 个人觉得还是不要使用中文作为文件名的好,虽然我们用的语言是中文,毕竟中文容易出现问题,兼容性不好。用Dreamweaver制作网页时,如果插
- 1. TVP, 表变量,临时表,CTE 的区别 TVP和临时表都是可以索引的,总是存在tempdb中,会增加系统数据库开销,而表变量和CTE
- 此BUG最初是在《前端观察》网站刊登,这里再描述一下,代码如下:<style>*{ padding:0; m
- 注入漏洞代码和分析<?php function customError($errno, $errstr, $errfile, $err
- PHP mysqli_rollback() 函数关闭自动提交,做一些查询,提交查询,然后回滚当前事务:<?php// 假定数据库用户名
- 挖坟 交互设计(Interaction Design)产生于二十世纪八十年代,在1984年一次设计会议上,大名鼎鼎的英国交互设计师比尔·莫格
- 如题,本次是要实现点击超链接实现执行js代码,并确认是否删除数据库数据,采用php。首先链接数据库,查询数据库数据:<?php$dbm