python對XML的解析


python有三種方法解析XML,SAX,DOM,以及ElementTree
###1.SAX (simple API for XML )
       pyhton 標准庫包含SAX解析器,SAX是一種典型的極為快速的工具,在解析XML時,不會占用大量內存。
但是這是基於回調機制的,因此在某些數據中,它會調用某些方法進行傳遞。這意味着必須為數據指定句柄,
以維持自己的狀態,這是非常困難的。


###2.DOM(Document Object Model)
       與SAX比較,DOM典型的缺點是比較慢,消耗更多的內存,因為DOM會將整個XML數讀入內存中,並為樹
中的第一個節點建立一個對象。使用DOM的好處是你不需要對狀態進行追蹤,因為每一個節點都知道誰是它的
父節點,誰是子節點。但是DOM用起來有些麻煩。


###3.ElementTree(元素樹)
     ElementTree就像一個輕量級的DOM,具有方便友好的API。代碼可用性好,速度快,消耗內存少,這里主要
介紹ElementTree。


下面是一個轉載的例子:

test.xml如下:

 

[html]  view plain copy
  1. <span style="font-size:13px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <root>  
  3.  <person age="18">  
  4.     <name>hzj</name>  
  5.     <sex>man</sex>  
  6.  </person>  
  7.  <person age="19" des="hello">  
  8.     <name>kiki</name>  
  9.     <sex>female</sex>  
  10.  </person>  
  11. </root></span>  

 

1.加載xml文件

    加載XML文件共有2種方法,一是加載指定字符串,二是加載指定文件

2.獲取element的方法

  a) 通過getiterator

  b) 過 getchildren

  c) find方法

  d) findall方法

[python]  view plain copy
  1. <span style="font-size:13px;">#-*- coding:utf-8 -*-  
  2. from xml.etree import ElementTree  
  3. def print_node(node):  
  4.     '''''打印結點基本信息'''  
  5.     print "=============================================="  
  6.     print "node.attrib:%s" % node.attrib  
  7.     if node.attrib.has_key("age") > 0 :  
  8.         print "node.attrib['age']:%s" % node.attrib['age']  
  9.     print "node.tag:%s" % node.tag  
  10.     print "node.text:%s" % node.text  
  11. def read_xml(text):  
  12.     '''''讀xml文件'''  
  13.     # 加載XML文件(2種方法,一是加載指定字符串,二是加載指定文件)      
  14.     # root = ElementTree.parse(r"D:/test.xml")  
  15.     root = ElementTree.fromstring(text)  
  16.       
  17.     # 獲取element的方法  
  18.     # 1 通過getiterator   
  19.     lst_node = root.getiterator("person")  
  20.     for node in lst_node:  
  21.         print_node(node)  
  22.           
  23.     # 2通過 getchildren  
  24.     lst_node_child = lst_node[0].getchildren()[0]  
  25.     print_node(lst_node_child)  
  26.           
  27.     # 3 .find方法  
  28.     node_find = root.find('person')  
  29.     print_node(node_find)  
  30.       
  31.     #4. findall方法  
  32.     node_findall = root.findall("person/name")[1]  
  33.     print_node(node_findall)  
  34.       
  35. if __name__ == '__main__':  
  36.      read_xml(open("test.xml").read())  
  37.  </span>  


想想為什么?不明白,請看下面

 

[python]  view plain copy
  1. #encoding=utf-8  
  2. from xml.etree import ElementTree as ET  
  3. #要找出所有人的年齡  
  4. per=ET.parse('test.xml')  
  5. p=per.findall('/person')  
  6. for x in p:  
  7.     print x.attrib  
  8. print  
  9. for oneper in p:  #找出person節點  
  10.     for child in oneper.getchildren(): #找出person節點的子節點  
  11.         print child.tag,':',child.text  
  12.   
  13.     print 'age:',oneper.get('age')  
  14.     print '############'  


結果如下:

[python]  view plain copy
  1. {'age''18'}  
  2. {'age''19''des''hello'}  
  3.   
  4. name : hzj  
  5. sex : man  
  6. age: 18  
  7. ############  
  8. name : kiki  
  9. sex : female  
  10. age: 19  
  11. ############ 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM