版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
第一文詳解測試Python讀寫xml配置文件前言:
xml也是常用的配置文件格式之一,Python中的xml.etree.ElementTree模塊支持解析和創(chuàng)建xml數(shù)據(jù)。xml格式不再贅述,本文采用參考文獻1中的示例xml數(shù)據(jù)作為測試數(shù)據(jù),
內(nèi)容如下:
xmlversion="1.0"
data
countryname="Liechtenstein"
rank1/rank
year2008/year
gdppc141100/gdppc
neighborname="Austria"direction="E"/
neighborname="Switzerland"direction="W"/
/country
countryname="Singapore"
rank4/rank
year2011/year
gdppc59900/gdppc
neighborname="Malaysia"direction="N"/
/country
countryname="Panama"
rank68/rank
year2011/year
gdppc13600/gdppc
neighborname="CostaRica"direction="W"/
neighborname="Colombia"direction="E"/
/country
/data
讀取xml文件時需要用到的對象及函數(shù)、屬性如下,完整的代碼及程序運行結(jié)果如下所示:
序號函數(shù)或?qū)傩哉f明1ElementTree.parse解析包含XML數(shù)據(jù)的文件名或文件對象,返回一個ElementTree實例2ElementTree.getroot返回ElementTree實例的根節(jié)點元素3Element.tag返回元素類型,也即當前標簽的名稱
4Element.attrib返回元素的屬性集合5Element.text返回元素內(nèi)容
importxml.etree.ElementTreeasET
defShowSubElement(curElement,level):
print(level,'tag:',curElement.tag,'attribute:',curElement.attrib,'text:',curElement.text)
forchildincurElement:
ShowSubElement(child,level+1)
tree=ET.parse('test.xml')
level=1
root=tree.getroot()
ShowSubElement(root,level)
構(gòu)建xml文件時需要用到的對象及函數(shù)、屬性如下,完整的代碼及程序運行結(jié)果如下所示:
序號函數(shù)或?qū)傩哉f明1ElementTree.write將ElementTree實例以XML格式寫入到文件,或是以寫入模式打開的fileobject2ElementTree.Element創(chuàng)建元素對象3Element.set設(shè)置元素的屬性
4Element.text設(shè)置元素內(nèi)容5Element.append將指定元素設(shè)置為當前元素的子元素6ElementTree.SubElement為給定元素創(chuàng)建新的子元素
importxml.etree.ElementTreeasET
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還不十分熟悉,則應(yīng)考慮改用xml.etree.ElementTree模塊來進行XML處理。
讀取xml文件時需要用到的對象及函數(shù)、屬性如下,完整的代碼及程序運行結(jié)果如下所示。從運行結(jié)果可以看出,xml.dom.minidom把元素的內(nèi)容也作為一個節(jié)點,即#text,這點來說,沒有xml.etree.ElementTree方便,后者不需要考慮這個。
序號函數(shù)或?qū)傩哉f明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é)點9A節(jié)點屬性的屬性名稱10Attr.value節(jié)點屬性的屬性值
fromxml.dom.minidomimportparse
defShowSubNode(curNode):
print('節(jié)點:',curNode.nodeName,":",curNode.nodeValue)
ifcurNode.nodeName=='#text':
return
ifcurNode.hasAttributes:
forattrincurNode.attributes.values():
print('屬性:',,':',attr.value)
ifcurNode.hasChildNodes:
forchildincurNode.childNodes:
ShowSubNode(child)
doc=parse('test.xml')
root=doc.documentElement
ShowSubNode(root)
構(gòu)建xml文件時需要用到的對象及函數(shù)、屬性如下,完整的代碼及程序運行結(jié)果如下所示:
序號函數(shù)或?qū)傩哉f明1xml.dom.minidom.Document()創(chuàng)建新的文檔對象2Document.createElement新建元素節(jié)點3Document.appendChild添加根節(jié)點
4Element.setAttribute新建節(jié)點屬性,同時設(shè)置屬性值5Element.appendChild添加子節(jié)點6Document.createTextNode創(chuàng)建文本節(jié)點7Document.writexml保存xml到文件
importxml.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('小明'))
withopen("writetest1.xml","w",encoding='utf-8')
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 我國商業(yè)銀行信貸資產(chǎn)證券化風險控制:理論、實踐與優(yōu)化路徑
- 區(qū)域金融合作協(xié)議
- 個人隱私信息授權(quán)使用同意書
- 一年級語文下冊必會、必看內(nèi)容
- 東財《工程項目管理》單元作業(yè)一
- 初中生寫作開頭結(jié)尾寫作技巧
- 三年級數(shù)學(xué)上冊期末考卷
- 教師職業(yè)道德規(guī)范與案例分析材料
- 中學(xué)語文萬能試講課件模板
- 三年級下冊語文考點復(fù)習資料大全
- 2026年山東城市服務(wù)職業(yè)學(xué)院單招職業(yè)技能考試題庫附答案詳解
- 研發(fā)部門員工加班管理細則
- 鋼結(jié)構(gòu)橋梁施工監(jiān)測方案
- 2025人教pep版三年級英語上冊字帖
- 《5G移動通信》課件-項目六 5G網(wǎng)絡(luò)中的人工智能技術(shù)
- 2025江蘇蘇州高新區(qū)獅山商務(wù)創(chuàng)新區(qū)下屬國有企業(yè)招聘9人筆試題庫及答案詳解
- 2025-2030中國城市青年租房行為特征與消費偏好調(diào)查報告
- 教培機構(gòu)年終工作總結(jié)
- 2025年秋季青島版三年級數(shù)學(xué)上冊求比一個數(shù)的幾倍多(少)幾的數(shù)教學(xué)課件
- 2025年法醫(yī)學(xué)法醫(yī)鑒定技能測試答案及解析
- 2025泰州中考數(shù)學(xué)試卷及答案
評論
0/150
提交評論