Python和xml简介
python提供越來(lái)越多的技術(shù)來(lái)支持xml,本文旨在面向初學(xué)利用Python處理xml的讀者,以教程的形式介紹一些基本的xml出來(lái)概念。前提是讀者必須知道一些xml常用術(shù)語(yǔ)。
先決條件
本文所有的例子基于Python2.6.5,pyxml的最新版本為0.8.1, 該教程中的例子都需要導(dǎo)入minidom模塊,所以在py文件中需要加入以下類似代碼:
?| 1 | import xml.dom.minidom |
當(dāng)然,你也可以從minidom模塊中只導(dǎo)入你需要的類。你可以使用以下代碼來(lái)查看該模塊的內(nèi)容:
?| 1 | dir(xml.dom.minidom) |
?
創(chuàng)建XML? 文件
首先,正如前面所說(shuō)的,導(dǎo)入minidom模塊:
?| 1 | import xml.dom.minidom |
要?jiǎng)?chuàng)建XML文件,我們需要Document這個(gè)對(duì)象的實(shí)例:
?| 1 2 | def get_a_document(): ????doc = xml.dom.minidom.Document() |
當(dāng)然這時(shí)候這個(gè)Document還沒(méi)有任何內(nèi)容,接下來(lái)我們將增加一些內(nèi)容到文件中。
元素節(jié)點(diǎn)(Elements)
XML文件中有一個(gè)唯一的‘根元素節(jié)點(diǎn)’,其他子元素節(jié)點(diǎn)以及文本內(nèi)容都是放在這個(gè)根元素的結(jié)構(gòu)中。這里我們可以創(chuàng)建一個(gè)xml文件,用于描述一個(gè) 公司的某個(gè)部分,該文件的根元素節(jié)點(diǎn)命名為“business”,名字空間(namespace)設(shè)置 為:http://www.boddie.org.uk/paul/business。代碼如下:
?| 1 | business_element = doc.createElementNS("http://www.boddie.org.uk/paul/business", "business") |
此刻我們已經(jīng)創(chuàng)建了元素節(jié)點(diǎn),但是還沒(méi)有加入到Document中,我們需要把它添加到文檔中:
?| 1 | doc.appendChild(business_element) |
最后在函數(shù)末尾返回我們創(chuàng)建的對(duì)象:
?| 1 | return doc, business_element |
為了便利,我把上面的代碼綜合起來(lái):
?| 1 2 3 4 5 | def get_a_document(): ????doc = xml.dom.minidom.Document() ????business_element = doc.createElementNS("http://www.boddie.org.uk/paul/business", "business") ????doc.appendChild(business_element) ????return doc, business_element |
執(zhí)行完上面的函數(shù),那么根元素節(jié)點(diǎn)已經(jīng)被添加Document中,我們可以通過(guò)查詢?cè)毓?jié)點(diǎn)信息:
>>> doc, business_element = get_a_document() >>> doc.childNodes [<DOM Element: business at 0x20ad0d0>]當(dāng)然你可以查詢這個(gè)節(jié)點(diǎn)列表里面的具體信息,
>>> doc.childNodes[0].namespaceURI 'http://www.boddie.org.uk/paul/business'最后查看下我們給元素節(jié)點(diǎn)設(shè)置的名字空間:
>>> doc.childNodes[0].localName 'business'business也就是我們剛才設(shè)置的根元素節(jié)點(diǎn)名字。有時(shí)候這個(gè)localName很重要,我們可以從中知道是公司的那個(gè)部門,同樣我們也可以像剛才那樣用一個(gè)函數(shù)來(lái)添加localName:
?| 1 2 | def add_a_location(doc, business_element): ????location_element = doc.createElementNS("http://www.boddie.org.uk/paul/business", "location") |
添加元素節(jié)點(diǎn)作為子節(jié)點(diǎn):
?| 1 | business_element.appendChild(location_element) |
最后返回:
?| 1 | return location_element |
有了以上這個(gè)函數(shù),我們就可以向根元素節(jié)點(diǎn)添加新的元素節(jié)點(diǎn):
>>> doc, business_element = get_a_document() >>> location_element = add_a_location(doc, business_element) >>> doc.childNodes [<DOM Element: business at 0x20dc5f8>]同樣我們也可以查看這個(gè)元素列表中更為詳細(xì)的信息:
>>> doc.childNodes[0].namespaceURI 'http://www.boddie.org.uk/paul/business' >>> doc.childNodes[0].localName 'business'文本
文本就是xml文件中的具體內(nèi)容,通常被置于xml元素標(biāo)簽中。緊接著前面的例子,我們將添加元素節(jié)點(diǎn)”surrounding”作為location的子節(jié)點(diǎn)。作用就是用于描述location那個(gè)地方的周邊環(huán)境。同樣我們創(chuàng)建一個(gè)函數(shù):
?| 1 2 3 | def add_surroundings(doc, location_element): ????surroundings_element = doc.createElementNS("http://www.boddie.org.uk/paul/business", "surroundings") ????location_element.appendChild(surroundings_element) |
然后添加文本內(nèi)容:
?| 1 | description = doc.createTextNode("A quiet, scenic park with lots of wildlife.") |
把這個(gè)文本節(jié)點(diǎn)添加到surrounding中:
?| 1 | surroundings_element.appendChild(description) |
返回對(duì)象:
?| 1 | return surroundings_element |
我們可以從跟元素節(jié)點(diǎn)查詢子節(jié)點(diǎn)信息:
>>> surroundings_element = add_surroundings(doc, location_element) >>> doc.childNodes[0].childNodes[0].childNodes[0].childNodes[0]當(dāng)然可以查看整個(gè)文本值:
>>> doc.childNodes[0].childNodes[0].childNodes[0].childNodes[0].nodeValue 'A quiet, scenic park with lots of wildlife.'同樣我們可以添加更多文本值:
?| 1 2 3 | def add_more_surroundings(doc, surroundings_element): ????description = doc.createTextNode(" It's usually sunny here, too.") ????surroundings_element.appendChild(description) |
來(lái)驗(yàn)證下結(jié)果:
>>> add_more_surroundings(doc, surroundings_element) >>> surroundings_element.childNodes [, , ]有時(shí)候我們需要把這段文本組合在一起,該如何做呢?
?| 1 2 | def fix_element(element): ????element.normalize() |
結(jié)果為:
>>> fix_element(surroundings_element) >>> surroundings_element.childNodes[0].nodeValue "A quiet, scenic park with lots of wildlife. It's usually sunny here, too. It's usually sunny here, too."屬性
xml 中的元素節(jié)點(diǎn)通常附帶有屬性。比如剛才的’location’節(jié)點(diǎn)還有一個(gè)屬性叫做’building’,這個(gè)元素的屬性名稱叫做’name’.
?| 1 2 | def add_building(doc, location_element): ????building_element = doc.createElementNS("http://www.boddie.org.uk/paul/business", "building") |
返回對(duì)象:
?| 1 2 | location_element.appendChild(building_element) return building_element |
這里我們注意到’building’同樣作為’location’的字節(jié)點(diǎn)出現(xiàn)在’surrounding’之后。我們可以用如下方法確認(rèn):
>>> building_element = add_building(doc, location_element) >>> location_element.childNodes [<DOM Element: surroundings at 136727844>, <DOM Element: building at 136286548>]這樣之后我們可以直接添加屬性:
?| 1 2 | def name_building(building_element): ????building_element.setAttributeNS("http://www.boddie.org.uk/paul/business", "business:name", "Ivory Tower") |
在名空間和元素節(jié)點(diǎn)以及文本值指定之后,我們還可以用以上方法添加其他屬性:
>>> name_building(building_element) >>> building_element.getAttributeNS("http://www.boddie.org.uk/paul/business", "name") 'Ivory Tower'寫XML文檔
當(dāng)你處理好以上x(chóng)ml內(nèi)容,通常需要保存起來(lái),所以一般是把內(nèi)容寫入文件。一個(gè)簡(jiǎn)單的方式是使用另外一個(gè)模塊:
?| 1 2 | import xml.dom.ext import xml.dom.minidom |
導(dǎo)入這兩個(gè)模塊,就有很多可用的函數(shù)和類,這里我們使用PrettyPrint函數(shù)輸出標(biāo)準(zhǔn)的xml結(jié)構(gòu):
?| 1 2 | def write_to_screen(doc): ????xml.dom.ext.PrettyPrint(doc) |
具體用法:
>>> from XML_intro.Writing import * >>> write_to_screen(doc) <?xml version='1.0' encoding='UTF-8'?> <business xmlns='http://www.boddie.org.uk/paul/business' xmlns:business='http://www.boddie.org.uk/paul/business'><location><surroundings>A quiet, scenic park with lots of wildlife.</surroundings><building business:name='Ivory Tower'/></location> </business>以上只是打印在屏幕上,最后完成輸出文件:
?| 1 2 3 4 | def write_to_file(doc, name="/tmp/doc.xml"): ????file_object = open(name, "w") ????xml.dom.ext.PrettyPrint(doc, file_object) ????file_object.close() |
或者簡(jiǎn)單的:
?| 1 2 | def write_to_file_easier(doc, name="/tmp/doc.xml"): ????xml.dom.ext.PrettyPrint(doc, open(name, "w")) |
讀XML文件
接下來(lái)講如何讀取上面保存的xml文件:
| 1 2 3 | import xml.dom.minidom def get_a_document(name="/tmp/doc.xml"): ????return xml.dom.minidom.parse(name) |
如果已經(jīng)存在存在xml可讀對(duì)象:
?| 1 2 | def get_a_document_from_file(file_object): ????return xml.dom.minidom.parse(file_object) |
更多資訊參考:http://www.boddie.org.uk/python/XML_intro.html
轉(zhuǎn)載于:https://www.cnblogs.com/mmix2009/p/3220987.html
總結(jié)
以上是生活随笔為你收集整理的Python和xml简介的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: poj 2749 2-SAT问题
- 下一篇: spoj 247