亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

一文詳解測試Python讀寫xml配置文件

 更新時間:2022年09月27日 17:02:53   作者:gc_2299  
這篇文章主要介紹了一文詳解測試Python讀寫xml配置文件,xml也是常用的配置文件格式之一,Python中的xml.etree.ElementTree模塊支持解析和創(chuàng)建xml數(shù)據(jù)

前言:

xml也是常用的配置文件格式之一,Python中的xml.etree.ElementTree模塊支持解析和創(chuàng)建xml數(shù)據(jù)。xml格式不再贅述,本文采用參考文獻1中的示例xml數(shù)據(jù)作為測試數(shù)據(jù),

內(nèi)容如下:

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>

讀取xml文件時需要用到的對象及函數(shù)、屬性如下,完整的代碼及程序運行結果如下所示:

序號函數(shù)或?qū)傩?/th>說明
1ElementTree.parse解析包含XML數(shù)據(jù)的文件名或文件對象,返回一個 ElementTree 實例
2ElementTree.getroot返回ElementTree 實例的根節(jié)點元素
3Element.tag返回元素類型,也即當前標簽的名稱
4Element.attrib返回元素的屬性集合
5Element.text返回元素內(nèi)容
import xml.etree.ElementTree as ET

def ShowSubElement(curElement,level):
    print(level,' tag:',curElement.tag,'attribute:', curElement.attrib,'text:',curElement.text)
    for child in curElement:
        ShowSubElement(child,level+1)
tree = ET.parse('test.xml')
level=1
root = tree.getroot()
ShowSubElement(root,level)

構建xml文件時需要用到的對象及函數(shù)、屬性如下,完整的代碼及程序運行結果如下所示:

序號函數(shù)或?qū)傩?/th>說明
1ElementTree.write將ElementTree實例以 XML 格式寫入到文件,或是以寫入模式打開的 file object
2ElementTree.Element創(chuàng)建元素對象
3Element.set設置元素的屬性
4Element.text設置元素內(nèi)容
5Element.append將指定元素設置為當前元素的子元素
6ElementTree.SubElement為給定元素創(chuàng)建新的子元素
import xml.etree.ElementTree as ET

root = ET.Element('root')
root.set('ver','2.0')

classA=ET.Element('classA')
root.append(classA)
classA.set("master","張三")
classA.set("grade","一年級")
studentA=ET.Element('studentA')
classA.append(studentA)
studentA.text='小米'

classB=ET.SubElement(root,'classB')
classB.set("master","李四")
classB.set("grade","三年級")
studentB=ET.SubElement(classB,'studentB')
studentB.text='小明'

tree = ET.ElementTree(root)
tree.write("writetest.xml")

除了上述基本的讀寫函數(shù)之外,xml.etree.ElementTree模塊還提供有很多十分方便的查找函數(shù),用于在 ElementTree 實例中快速查找指定的元素,詳細介紹請見參考,

除了xml.etree.ElementTree模塊,Python還支持采用xml.dom.minidom讀寫xml文件,后者是文檔對象模型接口的最小化實現(xiàn),其目標是比完整 DOM 更簡單并且更為小巧,但如果對于DOM 還不十分熟悉,則應考慮改用 xml.etree.ElementTree 模塊來進行 XML 處理。

讀取xml文件時需要用到的對象及函數(shù)、屬性如下,完整的代碼及程序運行結果如下所示。從運行結果可以看出,xml.dom.minidom把元素的內(nèi)容也作為一個節(jié)點,即#text,這點來說,沒有xml.etree.ElementTree方便,后者不需要考慮這個。

序號函數(shù)或?qū)傩?/th>說明
1xml.dom.minidom.parse根據(jù)給定的輸入返回Document對象,輸入?yún)?shù)可以是文件名,也可以是文件類對象,如果xml內(nèi)容保存在字符串中,可以使用parseString解析xml字符串
2Document.documentElement返回文檔根元素
3Node.nodeName獲取節(jié)點的節(jié)點名稱,也即元素類型
4Node.nodeValue獲取節(jié)點的值
5Node.attributes獲取節(jié)點的屬性集合
6Node.childNodes獲取節(jié)點的子節(jié)點集合
7Node.hasAttributes獲取節(jié)點是否有屬性
8Node.hasChildNodes獲取節(jié)點是否有子節(jié)點
9Attr.name節(jié)點屬性的屬性名稱
10Attr.value節(jié)點屬性的屬性值
from xml.dom.minidom import parse

def ShowSubNode(curNode):
    print('節(jié)點:',curNode.nodeName,":",curNode.nodeValue)

    if curNode.nodeName=='#text':
        return
    if curNode.hasAttributes:
        for attr in curNode.attributes.values():
            print('屬性:',attr.name,':',attr.value)

    if curNode.hasChildNodes:
        for child in curNode.childNodes:
            ShowSubNode(child)

doc = parse('test.xml')
root = doc.documentElement
ShowSubNode(root)

構建xml文件時需要用到的對象及函數(shù)、屬性如下,完整的代碼及程序運行結果如下所示:

序號函數(shù)或?qū)傩?/th>說明
1xml.dom.minidom.Document()創(chuàng)建新的文檔對象
2Document.createElement新建元素節(jié)點
3Document.appendChild添加根節(jié)點
4Element.setAttribute新建節(jié)點屬性,同時設置屬性值
5Element.appendChild添加子節(jié)點
6Document.createTextNode創(chuàng)建文本節(jié)點
7Document.writexml保存xml到文件
import xml.dom.minidom

doc = xml.dom.minidom.Document()

root=doc.createElement("root")
root.setAttribute('ver','2.0')
doc.appendChild(root) 

classA=doc.createElement('classA')
root.appendChild(classA)
classA.setAttribute("master","張三")
classA.setAttribute("grade","一年級")
studentA=doc.createElement('studentA')
classA.appendChild(studentA)
studentA.appendChild(doc.createTextNode('小米'))

classB=doc.createElement('classB')
root.appendChild(classB)
classB.setAttribute("master","李四")
classB.setAttribute("grade","三年級")
studentB=doc.createElement('studentB')
classB.appendChild(studentB)
studentB.appendChild(doc.createTextNode('小明'))

with open("writetest1.xml", "w", encoding='utf-8') as f:
    doc.writexml(f, indent='\t', addindent='\t', newl='\n', encoding="utf-8")

上述內(nèi)容即為采用xml.dom.minidom讀寫xml文件的基本用法。測試代碼主要參考自參考[1],[2],其中唯一需要說明的是枚舉節(jié)點的屬性集合,百度了很多文章都沒有看到怎么枚舉的,后面直接到xml.dom.minidom的源碼中翻到的用法參考

到此這篇關于一文詳解測試Python讀寫xml配置文件的文章就介紹到這了,更多相關Python讀寫xml配置文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論