http://www.cnblogs.com/itech/archive/2011/01/06/1924972.htmlhtml
一 python提供的xml支持
2種工業標準的xml解析方法-SAX和DOM。SAX(simple API for XML),是基於事件處理的,當XML文檔順序地讀入時,每次遇到一個元素會觸發相應的事件處理函數來處理。DOM(Document Object Model),經過構建一個樹結構來表現整個xml文檔,一旦樹被構建,能夠經過DOM提供了接口來遍歷樹和提取相應的數據。node
python還提供了python獨特的xml解析方法,相比於SAX和DOM更容易使用和更加快速,此方法爲ElementTree。python
python的xml模塊爲:linux
1)xml.dom.minidomwindows
2)xml.elementtreeapp
3)xml.sax + xml.domdom
二 xml實例:(employees.xml)函數
<?
xml version="1.0" encoding="UTF-8"
?>
<
employees
>
<
employee
>
<
name
>l
inux
</
name
>
<
age
>
30
</
age
>
</
employee
>
<
employee
>
<
name
>windows
</
name
>
<
age
>
20
</
age
>
</
employee
>
</
employees
>
三 使用xml.dom.minidom來讀寫xml編碼
1)使用xml.dom.minidom來解析xml:spa
def
TestMiniDom():
from
xml.dom
import
minidom
doc
=
minidom.parse(
"
employees.xml
"
)
#
get root element: <employees/>
root
=
doc.documentElement
#
get all children elements: <employee/> <employee/>
employees
=
root.getElementsByTagName(
"
employee
"
)
for
employee
in
employees:
print
(
"
-------------------------------------------
"
)
#
element name : employee
print
(employee.nodeName)
#
element xml content : <employee><name>windows</name><age>20</age></employee>
#
basically equal to toprettyxml function
print
(employee.toxml())
nameNode
=
employee.getElementsByTagName(
"
name
"
)[0]
print
(nameNode.childNodes)
print
(nameNode.nodeName
+
"
:
"
+
nameNode.childNodes[0].nodeValue)
ageNode
=
employee.getElementsByTagName(
"
age
"
)[0]
print
(ageNode.childNodes)
print
(ageNode.nodeName
+
"
:
"
+
ageNode.childNodes[0].nodeValue)
print
(
"
-------------------------------------------
"
)
#
children nodes : \n is one text element
#
[
#
<DOM Text node "' \n '">,
#
<DOM Element: name at 0xc9e490>,
#
<DOM Text node "'\n '">,
#
<DOM Element: age at 0xc9e4f0>,
#
<DOM Text node "'\n '">
#
]
for
n
in
employee.childNodes:
print
(n)
TestMiniDom()
運行結果:
-------------------------------------------
employee
<
employee
>
<
name
>
linux
</
name
>
<
age
>
30
</
age
>
</
employee
>
[
<
DOM
Text node "'linux'"
>
]
name:linux
[
<
DOM
Text node "'30'"
>
]
age:30
-------------------------------------------
<
DOM
Text node "' \n '"
>
<
DOM
Element: name at 0xc9f590
>
<
DOM
Text node "'\n '"
>
<
DOM
Element: age at 0xc9f5f0
>
<
DOM
Text node "'\n '"
>
-------------------------------------------
employee
<
employee
>
<
name
>
windows
</
name
>
<
age
>
20
</
age
>
</
employee
>
[
<
DOM
Text node "'windows'"
>
]
name:windows
[
<
DOM
Text node "'20'"
>
]
age:20
-------------------------------------------
<
DOM
Text node "' \n '"
>
<
DOM
Element: name at 0xc9f6b0
>
<
DOM
Text node "'\n '"
>
<
DOM
Element: age at 0xc9f710
>
<
DOM
Text node "'\n '"
>
2)使用xml.dom.minidom來生成xml:
def
GenerateXml():
import
xml.dom.minidom
impl
=
xml.dom.minidom.getDOMImplementation()
dom
=
impl.createDocument(None,
'
employees
'
, None)
root
=
dom.documentElement
employee
=
dom.createElement(
'
employee
'
)
root.appendChild(employee)
nameE
=
dom.createElement(
'
name
'
)
nameT
=
dom.createTextNode(
'
linux
'
)
nameE.appendChild(nameT)
employee.appendChild(nameE)
ageE
=
dom.createElement(
'
age
'
)
ageT
=
dom.createTextNode(
'
30
'
)
ageE.appendChild(ageT)
employee.appendChild(ageE)
f
=
open(
'
employees2.xml
'
,
'
w
'
, encoding
=
'
utf-8
'
)
dom.writexml(f,
addindent
=
'
'
, newl
=
'
\n
'
,encoding
=
'
utf-8
'
)
f.close()
GenerateXml()
運行結果:
<?
xml version="1.0" encoding="utf-8"
?>
<
employees
>
<
employee
>
<
name
>
linux
</
name
>
<
age
>
30
</
age
>
</
employee
>
</
employees
>
3)使用xml.dom.minidom須要注意的
*使用parse()或createDocument()返回的爲DOM對象;
*使用DOM的documentElement屬性能夠得到Root Element;
*DOM爲樹形結構,包含許多的nodes,其中element是node的一種,能夠包含子elements,textNode也是node的一種,是最終的子節點;
*每一個node都有nodeName,nodeValue,nodeType屬性,nodeValue是結點的值,只對textNode有效。對於textNode,想獲得它的文本內容能夠使用: .data屬性。
*nodeType是結點的類型,如今有如下:
'ATTRIBUTE_NODE''CDATA_SECTION_NODE''COMMENT_NODE''DOCUMENT_FRAGMENT_NODE'
'DOCUMENT_NODE''DOCUMENT_TYPE_NODE''ELEMENT_NODE''ENTITY_NODE''ENTITY_REFERENCE_NODE'
'NOTATION_NODE''PROCESSING_INSTRUCTION_NODE''TEXT_NODE'
*getElementsByTagName()能夠根據名字來查找子elements;
*childNodes返回全部的子Nodes,其中全部的文本均爲textNode,包含元素間的‘\n\r’和空格均爲textNode;
*writexml() 時addindent=' '表示子元素的縮進,newl='\n'表示元素間的換行,encoding='utf-8'表示生成的xml的編碼格式(<?xml version="1.0" encoding="utf-8"?>)。
參考:
http://boyeestudio.cnblogs.com/archive/2005/08/16/216408.html
http://www.dnbcw.com/biancheng/python/pnwb252539.html
http://blog.csdn.net/kiki113/archive/2009/04/15/4080789.aspx
完