当前位置:网站首页>Le module minidom écrit et analyse XML
Le module minidom écrit et analyse XML
2022-07-04 21:21:00 【Harry tsz】
Un.、Écris. XML Documentation
from xml.dom import minidom
# 1.CréationDOMObjet arborescent
dom=minidom.Document()
# 2.Créer un noeud racine.À chaque foisDOMObjet pour créer n'importe quel noeud.
root_node=dom.createElement('root')
# 3.AvecDOMObjet ajouter un noeud racine
dom.appendChild(root_node)
# AvecDOMObjet créer un noeud enfant d'élément
book_node=dom.createElement('book')
# Ajouter un noeud enfant primitif avec un objet parent
root_node.appendChild(book_node)
# Définir les propriétés de ce noeud
book_node.setAttribute('price','199')
name_node=dom.createElement('name')
root_node.appendChild(name_node)
# Ça marche aussi.DOMCréer un noeud de texte,Noeud de texte(Contenu du texte)Comme un noeud enfant
name_text=dom.createTextNode('Langage de programmation informatique No1Édition')
# Avec un objet de noeud avec du texte ajouté(Comme parent d'un noeud de texte)Ajouter un noeud de texte
name_node.appendChild(name_text)
# Chaque objet nodal (Y compris:domObjet lui - même) Ont produit XMLMéthodes de contenu,Par exemple::toxml()--String, toprettyxml()-- Embellir le format arborescent .
try:
with open('dom_write.xml','w',encoding='UTF-8') as fh:
# 4.writexml() Le premier paramètre est l'objet de fichier cible , Le deuxième paramètre est le format d'indentation du noeud racine , Le troisième paramètre est le format d'indentation des autres noeuds enfants ,
# Le quatrième paramètre établit un nouveau format de ligne , Le cinquième paramètre établit xmlCodage du contenu.
dom.writexml(fh,indent='',addindent='\t',newl='\n',encoding='UTF-8')
print('Écrirexml OK!')
except Exception as err:
print('Message d'erreur:{0}'.format(err))
Les résultats sont les suivants::
<?xml version="1.0" encoding="utf8"?>
<root>
<book price="99">
<name>Langage de programmation informatique No1Édition</name>
</book>
</root>
2.、Analyse XML Documentation
from xml.dom import minidom
with open('dom_write.xml','r',encoding='utf8') as fh:
# parse()AccèsDOMObjet
dom = minidom.parse(fh)
# Obtenir le noeud racine
root = dom.documentElement
# Nom du noeud
print(root.nodeName)
# Type de noeud:'ELEMENT_NODE',Noeud d'élément; 'TEXT_NODE',Noeud de texte; 'ATTRIBUTE_NODE',Noeud d'attribut
print(root.nodeType)
# Obtenir tous les noeuds enfants sous un noeud ,C'est une liste.
print(root.childNodes)
# Adoptiondom Objet ou élément racine , Puis obtenir le noeud d'élément à partir du nom de l'étiquette ,C'est une liste.
book = root.getElementsByTagName('book')[0]
# Obtenir les propriétés du noeud
print(book.getAttribute('price'))
# Obtenir le contenu textuel d'un noeud d'élément , Obtenez d'abord le noeud de texte Enfant ,Et à travers“data” Propriété obtenir le contenu du texte
name = root.getElementsByTagName('name')[0]
name_text_node = name.childNodes[0]
print(name_text_node.data)
# Obtenir le parent d'un noeud
print(name.parentNode.nodeName)
边栏推荐
- 实战模拟│JWT 登录认证
- 多模输入事件分发机制详解
- Billions of citizens' information has been leaked! Is there any "rescue" for data security on the public cloud?
- vim异步问题
- nmap扫描
- A quick start to fastdfs takes you three minutes to upload and download files to the ECS
- Day24: file system
- 测试员的算法面试题-找众数
- Actual combat simulation │ JWT login authentication
- maya灯建模
猜你喜欢
随机推荐
HWiNFO硬件检测工具v7.26绿色版
Gobang go to work fishing tools can be LAN / man-machine
shp数据制作3DTiles白膜
每日一题-LeetCode1200-最小绝对差-数组-排序
redis发布订阅的使用
多模輸入事件分發機制詳解
Flutter在 release版本,打开后随机白屏不显示内容
Routing configuration and connectivity test of Huawei simulator ENSP
Difference between ApplicationContext and beanfactory (MS)
HMS Core 机器学习服务
Huawei ENSP simulator enables devices of multiple routers to access each other
Jerry's ad series MIDI function description [chapter]
PS vertical English and digital text how to change direction (vertical display)
js 3D爆炸碎片图片切换js特效
【申博攻略】六.如何联系心仪的博导
Day24: file system
Explication détaillée du mécanisme de distribution des événements d'entrée multimodes
LeetCode 8. String conversion integer (ATOI)
杰理之AD 系列 MIDI 功能说明【篇】
[Shenbo introduction] VI How to contact your favorite doctoral tutor









