溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Python minidom模塊用法示例【DOM寫入和解析XML】

發(fā)布時(shí)間:2020-09-17 00:19:34 來源:腳本之家 閱讀:183 作者:薔薇Nina 欄目:開發(fā)技術(shù)

本文實(shí)例講述了Python minidom模塊用法。分享給大家供大家參考,具體如下:

一、DOM寫XML文件

# -*- coding:utf-8 -*-
#!python3
#導(dǎo)入minidom
from xml.dom import minidom
# 1.創(chuàng)建DOM樹對(duì)象
dom=minidom.Document()
# 2.創(chuàng)建根節(jié)點(diǎn)。每次都要用DOM對(duì)象來創(chuàng)建任何節(jié)點(diǎn)。
root_node=dom.createElement('root')
# 3.用DOM對(duì)象添加根節(jié)點(diǎn)
dom.appendChild(root_node)
# 用DOM對(duì)象創(chuàng)建元素子節(jié)點(diǎn)
book_node=dom.createElement('book')
# 用父節(jié)點(diǎn)對(duì)象添加元素子節(jié)點(diǎn)
root_node.appendChild(book_node)
# 設(shè)置該節(jié)點(diǎn)的屬性
book_node.setAttribute('price','199')
name_node=dom.createElement('name')
root_node.appendChild(name_node)
# 也用DOM創(chuàng)建文本節(jié)點(diǎn),把文本節(jié)點(diǎn)(文字內(nèi)容)看成子節(jié)點(diǎn)
name_text=dom.createTextNode('計(jì)算機(jī)程序設(shè)計(jì)語言 第1版')
# 用添加了文本的節(jié)點(diǎn)對(duì)象(看成文本節(jié)點(diǎn)的父節(jié)點(diǎn))添加文本節(jié)點(diǎn)
name_node.appendChild(name_text)
# 每一個(gè)結(jié)點(diǎn)對(duì)象(包括dom對(duì)象本身)都有輸出XML內(nèi)容的方法,如:toxml()--字符串, toprettyxml()--美化樹形格式。
try:
  with open('dom_write.xml','w',encoding='UTF-8') as fh:
    # 4.writexml()第一個(gè)參數(shù)是目標(biāo)文件對(duì)象,第二個(gè)參數(shù)是根節(jié)點(diǎn)的縮進(jìn)格式,第三個(gè)參數(shù)是其他子節(jié)點(diǎn)的縮進(jìn)格式,
    # 第四個(gè)參數(shù)制定了換行格式,第五個(gè)參數(shù)制定了xml內(nèi)容的編碼。
    dom.writexml(fh,indent='',addindent='\t',newl='\n',encoding='UTF-8')
    print('寫入xml OK!')
except Exception as err:
  print('錯(cuò)誤信息:{0}'.format(err))

生成的dom_write.xml文件結(jié)果如下:

<?xml version="1.0" encoding="UTF-8"?>
<root>
 <book price="199"/>
 <name>計(jì)算機(jī)程序設(shè)計(jì)語言 第1版</name>
</root>

二、DOM解析XML文件

# -*- coding:utf-8 -*-
#!python3
from xml.dom import minidom
with open('dom_write.xml','r',encoding='utf8') as fh:
  # parse()獲取DOM對(duì)象
  dom=minidom.parse(fh)
  # 獲取根節(jié)點(diǎn)
  root=dom.documentElement
  # 節(jié)點(diǎn)名稱
  print(root.nodeName)
  # 節(jié)點(diǎn)類型:'ELEMENT_NODE',元素節(jié)點(diǎn); 'TEXT_NODE',文本節(jié)點(diǎn); 'ATTRIBUTE_NODE',屬性節(jié)點(diǎn)
  print(root.nodeType)
  # 獲取某個(gè)節(jié)點(diǎn)下所有子節(jié)點(diǎn),是個(gè)列表
  print(root.childNodes)
  # 通過dom對(duì)象或根元素,再根據(jù)標(biāo)簽名獲取元素節(jié)點(diǎn),是個(gè)列表
  book=root.getElementsByTagName('book')[0]
  # 獲取節(jié)點(diǎn)屬性
  print(book.getAttribute('price'))
  # 獲取某個(gè)元素節(jié)點(diǎn)的文本內(nèi)容,先獲取子文本節(jié)點(diǎn),然后通過“data”屬性獲取文本內(nèi)容
  name=root.getElementsByTagName('name')[0]
  name_text_node=name.childNodes[0]
  print(name_text_node.data)
  # 獲取某節(jié)點(diǎn)的父節(jié)點(diǎn)
  print(name.parentNode.nodeName)

運(yùn)行輸出:

root
1
[<DOM Text node "'\n\t'">, <DOM Element: book at 0x1dd2800>, <DOM Text node "'\n\t'">, <DOM Element: name at 0x1dd2850>, <DOM Text node "'\n'">]
199
計(jì)算機(jī)程序設(shè)計(jì)語言 第1版
root

PS:這里再為大家提供幾款關(guān)于xml操作的在線工具供大家參考使用:

在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson

在線格式化XML/在線壓縮XML
http://tools.jb51.net/code/xmlformat

XML在線壓縮/格式化工具:
http://tools.jb51.net/code/xml_format_compress

XML代碼在線格式化美化工具:
http://tools.jb51.net/code/xmlcodeformat

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python操作xml數(shù)據(jù)技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI