python 之 XML的基本应用总结
生活随笔
收集整理的這篇文章主要介紹了
python 之 XML的基本应用总结
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1.XML 的特征:xml即可擴(kuò)展標(biāo)記語(yǔ)言,它可以用來(lái)標(biāo)記數(shù)據(jù)、定義數(shù)據(jù)類(lèi)型,是一種允許用戶(hù)對(duì)自己的標(biāo)記語(yǔ)言進(jìn)行定義的源語(yǔ)言。從結(jié)構(gòu)上,很像HTML超文本標(biāo)記語(yǔ)言。但他們被設(shè)計(jì)的目的是不同的,超文本標(biāo)記語(yǔ)言被設(shè)計(jì)用來(lái)顯示數(shù)據(jù),其焦點(diǎn)是數(shù)據(jù)的外觀。它被設(shè)計(jì)用來(lái)傳輸和存儲(chǔ)數(shù)據(jù),其焦點(diǎn)是數(shù)據(jù)的內(nèi)容
?
那么它有如下特征:
?
- 它是有標(biāo)簽對(duì)組成,<aa></aa>
- 標(biāo)簽可以有屬性:<aa id='123'></aa>
- 標(biāo)簽對(duì)可以嵌入數(shù)據(jù):<aa>abc</aa>
- 標(biāo)簽可以嵌入子標(biāo)簽(具有層級(jí)關(guān)系)
?
例子:創(chuàng)建一個(gè)XML文件
?
<?xml version="1.0"?> <data><country name="Liechtenstein"><rank updated="yes">2</rank><year>2008</year><gdppc>141100</gdppc><neighbor name="Austria" direction="E"/><neighbor name="Switzerland" direction="W"/></country><country name="Singapore"><rank updated="yes">5</rank><year>2011</year><gdppc>59900</gdppc><neighbor name="Malaysia" direction="N"/></country><country name="Panama"><rank updated="yes">69</rank><year>2011</year><gdppc>13600</gdppc><neighbor name="Costa Rica" direction="W"/><neighbor name="Colombia" direction="E"/></country> </data>?
步驟:
【XML操作】
?
import xml.etree.ElementTree as ETtree = ET.parse("xmltest.xml") root = tree.getroot() print(root.tag)#遍歷xml文檔 for child in root:print(child.tag, child.attrib)for i in child:print(i.tag,i.text)#只遍歷year 節(jié)點(diǎn) for node in root.iter('year'):print(node.tag,node.text) #修改和刪除xml文檔內(nèi)容 import xml.etree.ElementTree as ETtree = ET.parse("xmltest.xml") root = tree.getroot()#修改 for node in root.iter('year'):new_year = int(node.text) + 1node.text = str(new_year)node.set("updated","yes")tree.write("xmltest.xml")#刪除node for country in root.findall('country'):rank = int(country.find('rank').text)if rank > 50:root.remove(country)tree.write('output.xml')【自己創(chuàng)建xml文檔】
import xml.etree.ElementTree as ETnew_xml = ET.Element("namelist") name = ET.SubElement(new_xml,"name",attrib={"enrolled":"yes"}) age = ET.SubElement(name,"age",attrib={"checked":"no"}) sex = ET.SubElement(name,"sex") sex.text = '33' name2 = ET.SubElement(new_xml,"name",attrib={"enrolled":"no"}) age = ET.SubElement(name2,"age") age.text = '19'et = ET.ElementTree(new_xml) #生成文檔對(duì)象 et.write("test.xml", encoding="utf-8",xml_declaration=True)ET.dump(new_xml) #打印生成的格式?總結(jié)
minidom.parse(filename) 加載讀取XML文件doc.documentElement 獲取XML文檔對(duì)象node.getAttribute(AttributeName) 獲取XML節(jié)點(diǎn)屬性值node.getElementsByTagName(TagName) 獲取XML節(jié)點(diǎn)對(duì)象集合node.childNodes #返回子節(jié)點(diǎn)列表。 node.childNodes[index].nodeValue 獲取XML節(jié)點(diǎn)值node.firstChild #訪問(wèn)第一個(gè)節(jié)點(diǎn)。等價(jià)于pagexml.childNodes[0] doc = minidom.parse(filename) doc.toxml('UTF-8') 返回Node節(jié)點(diǎn)的xml表示的文本Node.attributes["id"] a.name #就是上面的 "id" a.value #屬性的值 訪問(wèn)元素屬性?
?
轉(zhuǎn)載于:https://www.cnblogs.com/lanyinhao/p/9176274.html
總結(jié)
以上是生活随笔為你收集整理的python 之 XML的基本应用总结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 浅学JavaScript
- 下一篇: iOS组件化-带你一步步实现项目的组件化