当前位置:网站首页>Minidom module writes and parses XML
Minidom module writes and parses XML
2022-07-04 21:22:00 【Harrytsz】
One 、 Write XML file
from xml.dom import minidom
# 1. establish DOM Tree object
dom=minidom.Document()
# 2. Create a root node . Use... Every time DOM Object to create any node .
root_node=dom.createElement('root')
# 3. use DOM Object to add the root node
dom.appendChild(root_node)
# use DOM Object to create element child nodes
book_node=dom.createElement('book')
# Add element child nodes with parent node objects
root_node.appendChild(book_node)
# Set the properties of this node
book_node.setAttribute('price','199')
name_node=dom.createElement('name')
root_node.appendChild(name_node)
# Also used DOM Create text node , Put the text node ( Written content ) As a child node
name_text=dom.createTextNode(' Computer programming language The first 1 edition ')
# Use the node object with text added ( As the parent node of the text node ) Add text node
name_node.appendChild(name_text)
# Every node object ( Include dom Object itself ) They all have output XML Method of content , Such as :toxml()-- character string , toprettyxml()-- Beautify the tree format .
try:
with open('dom_write.xml','w',encoding='UTF-8') as fh:
# 4.writexml() The first parameter is the target file object , The second parameter is the indentation format of the root node , The third parameter is the indent format of other child nodes ,
# The fourth parameter sets the line feed format , The fifth parameter establishes xml Coding of content .
dom.writexml(fh,indent='',addindent='\t',newl='\n',encoding='UTF-8')
print(' write in xml OK!')
except Exception as err:
print(' error message :{0}'.format(err))
give the result as follows :
<?xml version="1.0" encoding="utf8"?>
<root>
<book price="99">
<name> Computer programming language The first 1 edition </name>
</book>
</root>
Two 、 analysis XML file
from xml.dom import minidom
with open('dom_write.xml','r',encoding='utf8') as fh:
# parse() obtain DOM object
dom = minidom.parse(fh)
# Get root node
root = dom.documentElement
# The name of the node
print(root.nodeName)
# Node type :'ELEMENT_NODE', Element nodes ; 'TEXT_NODE', Text node ; 'ATTRIBUTE_NODE', Attribute node
print(root.nodeType)
# Get all the children under a node , It's a list
print(root.childNodes)
# adopt dom Object or root element , Then get the element node according to the tag name , It's a list
book = root.getElementsByTagName('book')[0]
# Get node properties
print(book.getAttribute('price'))
# Get the text content of an element node , Get the sub text node first , And then through “data” Property to get the text content
name = root.getElementsByTagName('name')[0]
name_text_node = name.childNodes[0]
print(name_text_node.data)
# Get the parent node of a node
print(name.parentNode.nodeName)
边栏推荐
- redis RDB AOF
- 基于OpenCV haarcascades的对象检测
- 杰理之AD 系列 MIDI 功能说明【篇】
- 【Try to Hack】宽字节注入
- 华为ensp模拟器实现通信安全(交换机)
- 2021 CCPC Harbin B. magical subsequence (thinking question)
- Use of redis publish subscription
- Difference between ApplicationContext and beanfactory (MS)
- [1200. Minimum absolute difference]
- 【optimtool.unconstrain】无约束优化工具箱
猜你喜欢

网件r7000梅林系统虚拟内存创建失败,提示USB磁盘读写速度不满足要求解决办法,有需要创建虚拟内存吗??

JS卡牌样式倒计时天数

heatmap.js图片热点热力图插件
![[1200. Différence absolue minimale]](/img/fa/4ffbedd8f24c75a20d3eaeaf0430ae.png)
[1200. Différence absolue minimale]

Some suggestions for interface design

shp数据制作3DTiles白膜

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

Detailed explanation of multi-mode input event distribution mechanism

PS vertical English and digital text how to change direction (vertical display)
![Jerry's ad series MIDI function description [chapter]](/img/28/e0f9b41db597ff3288af431c677001.png)
Jerry's ad series MIDI function description [chapter]
随机推荐
Why does invariant mode improve performance
数十亿公民信息遭泄漏!公有云上的数据安全还有“救”吗?
VIM asynchronous problem
Detailed explanation of multi-mode input event distribution mechanism
Huawei ENSP simulator enables devices of multiple routers to access each other
冰河的海报封面
redis发布订阅的使用
PS vertical English and digital text how to change direction (vertical display)
Foxit pdf editor v10.1.8 green version
Jerry's ad series MIDI function description [chapter]
Hwinfo hardware detection tool v7.26 green version
Day24:文件系统
华为ensp模拟器 实现多个路由器的设备可以相互访问
Pytorch---使用Pytorch实现LinkNet进行语义分割
TweenMax表情按钮js特效
Gobang go to work fishing tools can be LAN / man-machine
Stealing others' vulnerability reports and selling them into sidelines, and the vulnerability reward platform gives rise to "insiders"
Le module minidom écrit et analyse XML
ApplicationContext 与 BeanFactory 区别(MS)
福昕PDF编辑器v10.1.8绿色版