欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

一文詳解測(cè)試Python讀寫xml配置文件

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

前言:

xml也是常用的配置文件格式之一,Python中的xml.etree.ElementTree模塊支持解析和創(chuàng)建xml數(shù)據(jù)。xml格式不再贅述,本文采用參考文獻(xiàn)1中的示例xml數(shù)據(jù)作為測(cè)試數(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í)需要用到的對(duì)象及函數(shù)、屬性如下,完整的代碼及程序運(yùn)行結(jié)果如下所示:

序號(hào)函數(shù)或?qū)傩?/th>說明
1ElementTree.parse解析包含XML數(shù)據(jù)的文件名或文件對(duì)象,返回一個(gè) ElementTree 實(shí)例
2ElementTree.getroot返回ElementTree 實(shí)例的根節(jié)點(diǎn)元素
3Element.tag返回元素類型,也即當(dāng)前標(biāo)簽的名稱
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)

構(gòu)建xml文件時(shí)需要用到的對(duì)象及函數(shù)、屬性如下,完整的代碼及程序運(yùn)行結(jié)果如下所示:

序號(hào)函數(shù)或?qū)傩?/th>說明
1ElementTree.write將ElementTree實(shí)例以 XML 格式寫入到文件,或是以寫入模式打開的 file object
2ElementTree.Element創(chuàng)建元素對(duì)象
3Element.set設(shè)置元素的屬性
4Element.text設(shè)置元素內(nèi)容
5Element.append將指定元素設(shè)置為當(dāng)前元素的子元素
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","一年級(jí)")
studentA=ET.Element('studentA')
classA.append(studentA)
studentA.text='小米'

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

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

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

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

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

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

def ShowSubNode(curNode):
    print('節(jié)點(diǎn):',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)

構(gòu)建xml文件時(shí)需要用到的對(duì)象及函數(shù)、屬性如下,完整的代碼及程序運(yùn)行結(jié)果如下所示:

序號(hào)函數(shù)或?qū)傩?/th>說明
1xml.dom.minidom.Document()創(chuàng)建新的文檔對(duì)象
2Document.createElement新建元素節(jié)點(diǎn)
3Document.appendChild添加根節(jié)點(diǎn)
4Element.setAttribute新建節(jié)點(diǎn)屬性,同時(shí)設(shè)置屬性值
5Element.appendChild添加子節(jié)點(diǎn)
6Document.createTextNode創(chuàng)建文本節(jié)點(diǎn)
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","一年級(jí)")
studentA=doc.createElement('studentA')
classA.appendChild(studentA)
studentA.appendChild(doc.createTextNode('小米'))

classB=doc.createElement('classB')
root.appendChild(classB)
classB.setAttribute("master","李四")
classB.setAttribute("grade","三年級(jí)")
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文件的基本用法。測(cè)試代碼主要參考自參考[1],[2],其中唯一需要說明的是枚舉節(jié)點(diǎn)的屬性集合,百度了很多文章都沒有看到怎么枚舉的,后面直接到xml.dom.minidom的源碼中翻到的用法參考

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

相關(guān)文章

最新評(píng)論