当前位置:网站首页>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)
边栏推荐
- 杰理之AD 系列 MIDI 功能说明【篇】
- How does wincc7.5 SP1 find variables and their positions through cross indexing?
- Jerry's ad series MIDI function description [chapter]
- 网件r7000梅林系统5g不稳定 5g信号经常掉线解决方法
- Foxit pdf editor v10.1.8 green version
- Jerry's ad series MIDI function description [chapter]
- In the release version, the random white screen does not display the content after opening the shutter
- 6月“墨力原创作者计划”获奖名单公布!邀您共话国产数据库
- PS竖排英文和数字文字怎么改变方向(变竖直显示)
- Huawei ENSP simulator realizes communication security (switch)
猜你喜欢
随机推荐
Liu Jincheng won the 2022 China e-commerce industry innovation Figure Award
WGCNA analysis basic tutorial summary
A quick start to fastdfs takes you three minutes to upload and download files to the ECS
杰理之AD 系列 MIDI 功能说明【篇】
redis RDB AOF
搭建一个仪式感点满的网站,并内网穿透发布到公网 1/2
杰理之AD 系列 MIDI 功能说明【篇】
HWiNFO硬件检测工具v7.26绿色版
Redis pipeline
redis事务
Vue cleans up the keepalive cache scheme in a timely manner
__init__() missing 2 required positional arguments 不易查明的继承错误
Routing configuration and connectivity test of Huawei simulator ENSP
嵌入式TC 测试用例
杰理之AD 系列 MIDI 功能说明【篇】
【Try to Hack】宽字节注入
Explication détaillée du mécanisme de distribution des événements d'entrée multimodes
Foxit pdf editor v10.1.8 green version
Poster cover of glacier
Embedded TC test case






![Jerry's ad series MIDI function description [chapter]](/img/28/e0f9b41db597ff3288af431c677001.png)


