当前位置:网站首页>minidom 模块写入和解析 XML
minidom 模块写入和解析 XML
2022-07-04 20:26:00 【Harrytsz】
一、写 XML 文件
from xml.dom import minidom
# 1.创建DOM树对象
dom=minidom.Document()
# 2.创建根节点。每次都要用DOM对象来创建任何节点。
root_node=dom.createElement('root')
# 3.用DOM对象添加根节点
dom.appendChild(root_node)
# 用DOM对象创建元素子节点
book_node=dom.createElement('book')
# 用父节点对象添加元素子节点
root_node.appendChild(book_node)
# 设置该节点的属性
book_node.setAttribute('price','199')
name_node=dom.createElement('name')
root_node.appendChild(name_node)
# 也用DOM创建文本节点,把文本节点(文字内容)看成子节点
name_text=dom.createTextNode('计算机程序设计语言 第1版')
# 用添加了文本的节点对象(看成文本节点的父节点)添加文本节点
name_node.appendChild(name_text)
# 每一个结点对象(包括dom对象本身)都有输出XML内容的方法,如:toxml()--字符串, toprettyxml()--美化树形格式。
try:
with open('dom_write.xml','w',encoding='UTF-8') as fh:
# 4.writexml()第一个参数是目标文件对象,第二个参数是根节点的缩进格式,第三个参数是其他子节点的缩进格式,
# 第四个参数制定了换行格式,第五个参数制定了xml内容的编码。
dom.writexml(fh,indent='',addindent='\t',newl='\n',encoding='UTF-8')
print('写入xml OK!')
except Exception as err:
print('错误信息:{0}'.format(err))
结果如下:
<?xml version="1.0" encoding="utf8"?>
<root>
<book price="99">
<name>计算机程序设计语言 第1版</name>
</book>
</root>
二、解析 XML 文件
from xml.dom import minidom
with open('dom_write.xml','r',encoding='utf8') as fh:
# parse()获取DOM对象
dom = minidom.parse(fh)
# 获取根节点
root = dom.documentElement
# 节点名称
print(root.nodeName)
# 节点类型:'ELEMENT_NODE',元素节点; 'TEXT_NODE',文本节点; 'ATTRIBUTE_NODE',属性节点
print(root.nodeType)
# 获取某个节点下所有子节点,是个列表
print(root.childNodes)
# 通过dom对象或根元素,再根据标签名获取元素节点,是个列表
book = root.getElementsByTagName('book')[0]
# 获取节点属性
print(book.getAttribute('price'))
# 获取某个元素节点的文本内容,先获取子文本节点,然后通过“data”属性获取文本内容
name = root.getElementsByTagName('name')[0]
name_text_node = name.childNodes[0]
print(name_text_node.data)
# 获取某节点的父节点
print(name.parentNode.nodeName)
边栏推荐
- 实战模拟│JWT 登录认证
- 【观察】联想:3X(1+N)智慧办公解决方案,释放办公生产力“乘数效应”
- HMS Core 机器学习服务
- D3.js+Three.js数据可视化3d地球js特效
- Go notes (3) usage of go language FMT package
- NetWare r7000 Merlin system virtual memory creation failed, prompting that the USB disk reading and writing speed does not meet the requirements. Solution, is it necessary to create virtual memory??
- Day24:文件系统
- 【微信小程序】协同工作与发布
- acwing 3302. 表达式求值
- 为什么说不变模式可以提高性能
猜你喜欢

Gobang go to work fishing tools can be LAN / man-machine

Huawei ENSP simulator enables devices of multiple routers to access each other

Huawei ENSP simulator configures ACL access control list

Ten years' experience of byte test engineer directly hits the pain point of UI automation test

A quick start to fastdfs takes you three minutes to upload and download files to the ECS

测试员的算法面试题-找众数

Billions of citizens' information has been leaked! Is there any "rescue" for data security on the public cloud?

The video sound of station B is very low - solution

How does wincc7.5 SP1 find variables and their positions through cross indexing?

每日一题-LeetCode1200-最小绝对差-数组-排序
随机推荐
In the face of the same complex test task, why can the elder sort out the solution quickly? Ali's ten-year test engineers showed their skills
华为模拟器ensp常用命令
Nmap scan
Ten years' experience of byte test engineer directly hits the pain point of UI automation test
acwing 3302. Expression evaluation
测试员的算法面试题-找众数
杰理之AD 系列 MIDI 功能说明【篇】
[observation] Lenovo: 3x (1+n) smart office solution, releasing the "multiplier effect" of office productivity
go defer的使用说明
2021 CCPC 哈尔滨 I. Power and Zero(二进制 + 思维)
Redis cache
Redis pipeline
Huawei ENSP simulator layer 3 switch
GVM use
杰理之AD 系列 MIDI 功能说明【篇】
【Try to Hack】宽字节注入
PS竖排英文和数字文字怎么改变方向(变竖直显示)
网件r7000梅林系统5g不稳定 5g信号经常掉线解决方法
LeetCode 7. 整数反转
WinCC7.5 SP1如何通过交叉索引来寻找变量及其位置?