Python和xml简介
python提供越來越多的技術(shù)來支持xml,本文旨在面向初學利用Python處理xml的讀者,以教程的形式介紹一些基本的xml出來概念。前提是讀者必須知道一些xml常用術(shù)語。
先決條件
本文所有的例子基于Python2.6.5,pyxml的最新版本為0.8.1, 該教程中的例子都需要導入minidom模塊,所以在py文件中需要加入以下類似代碼:
?| 1 | import xml.dom.minidom |
當然,你也可以從minidom模塊中只導入你需要的類。你可以使用以下代碼來查看該模塊的內(nèi)容:
?| 1 | dir(xml.dom.minidom) |
?
創(chuàng)建XML? 文件
首先,正如前面所說的,導入minidom模塊:
?| 1 | import xml.dom.minidom |
要創(chuàng)建XML文件,我們需要Document這個對象的實例:
?| 1 2 | def get_a_document(): ????doc = xml.dom.minidom.Document() |
當然這時候這個Document還沒有任何內(nèi)容,接下來我們將增加一些內(nèi)容到文件中。
元素節(jié)點(Elements)
XML文件中有一個唯一的‘根元素節(jié)點’,其他子元素節(jié)點以及文本內(nèi)容都是放在這個根元素的結(jié)構(gòu)中。這里我們可以創(chuàng)建一個xml文件,用于描述一個 公司的某個部分,該文件的根元素節(jié)點命名為“business”,名字空間(namespace)設置 為: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é)點,但是還沒有加入到Document中,我們需要把它添加到文檔中:
?| 1 | doc.appendChild(business_element) |
最后在函數(shù)末尾返回我們創(chuàng)建的對象:
?| 1 | return doc, business_element |
為了便利,我把上面的代碼綜合起來:
?| 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é)點已經(jīng)被添加Document中,我們可以通過查詢元素節(jié)點信息:
>>> doc, business_element = get_a_document() >>> doc.childNodes [<DOM Element: business at 0x20ad0d0>]當然你可以查詢這個節(jié)點列表里面的具體信息,
>>> doc.childNodes[0].namespaceURI 'http://www.boddie.org.uk/paul/business'最后查看下我們給元素節(jié)點設置的名字空間:
>>> doc.childNodes[0].localName 'business'business也就是我們剛才設置的根元素節(jié)點名字。有時候這個localName很重要,我們可以從中知道是公司的那個部門,同樣我們也可以像剛才那樣用一個函數(shù)來添加localName:
?| 1 2 | def add_a_location(doc, business_element): ????location_element = doc.createElementNS("http://www.boddie.org.uk/paul/business", "location") |
添加元素節(jié)點作為子節(jié)點:
?| 1 | business_element.appendChild(location_element) |
最后返回:
?| 1 | return location_element |
有了以上這個函數(shù),我們就可以向根元素節(jié)點添加新的元素節(jié)點:
>>> doc, business_element = get_a_document() >>> location_element = add_a_location(doc, business_element) >>> doc.childNodes [<DOM Element: business at 0x20dc5f8>]同樣我們也可以查看這個元素列表中更為詳細的信息:
>>> doc.childNodes[0].namespaceURI 'http://www.boddie.org.uk/paul/business' >>> doc.childNodes[0].localName 'business'文本
文本就是xml文件中的具體內(nèi)容,通常被置于xml元素標簽中。緊接著前面的例子,我們將添加元素節(jié)點”surrounding”作為location的子節(jié)點。作用就是用于描述location那個地方的周邊環(huán)境。同樣我們創(chuàng)建一個函數(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.") |
把這個文本節(jié)點添加到surrounding中:
?| 1 | surroundings_element.appendChild(description) |
返回對象:
?| 1 | return surroundings_element |
我們可以從跟元素節(jié)點查詢子節(jié)點信息:
>>> surroundings_element = add_surroundings(doc, location_element) >>> doc.childNodes[0].childNodes[0].childNodes[0].childNodes[0]當然可以查看整個文本值:
>>> 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) |
來驗證下結(jié)果:
>>> add_more_surroundings(doc, surroundings_element) >>> surroundings_element.childNodes [, , ]有時候我們需要把這段文本組合在一起,該如何做呢?
?| 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é)點通常附帶有屬性。比如剛才的’location’節(jié)點還有一個屬性叫做’building’,這個元素的屬性名稱叫做’name’.
?| 1 2 | def add_building(doc, location_element): ????building_element = doc.createElementNS("http://www.boddie.org.uk/paul/business", "building") |
返回對象:
?| 1 2 | location_element.appendChild(building_element) return building_element |
這里我們注意到’building’同樣作為’location’的字節(jié)點出現(xiàn)在’surrounding’之后。我們可以用如下方法確認:
>>> 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é)點以及文本值指定之后,我們還可以用以上方法添加其他屬性:
>>> name_building(building_element) >>> building_element.getAttributeNS("http://www.boddie.org.uk/paul/business", "name") 'Ivory Tower'寫XML文檔
當你處理好以上xml內(nèi)容,通常需要保存起來,所以一般是把內(nèi)容寫入文件。一個簡單的方式是使用另外一個模塊:
?| 1 2 | import xml.dom.ext import xml.dom.minidom |
導入這兩個模塊,就有很多可用的函數(shù)和類,這里我們使用PrettyPrint函數(shù)輸出標準的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() |
或者簡單的:
?| 1 2 | def write_to_file_easier(doc, name="/tmp/doc.xml"): ????xml.dom.ext.PrettyPrint(doc, open(name, "w")) |
讀XML文件
接下來講如何讀取上面保存的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可讀對象:
?| 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简介的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: poj 2749 2-SAT问题
- 下一篇: websocket python爬虫_p