2012-11-20 23 views
4

我是新來的python,並想了解解析XML。我一直無法找到任何有關如何創建通用程序來遍歷XML節點集的好例子或解釋。在Python中走的XML

我希望能夠根據名稱和值對所有元素和屬性進行分類和標識,而不需要任何有關xml模式的信息。我不想依靠標籤名稱或文本特別調用元素和屬性。

難道有人請指點我正確的方向嗎?

感謝

UPDATE:

,目前正在問的是,具體的問題:「我怎麼一般遞歸所有節點從XML文檔中的根節點,而無需對架構進行任何深入的瞭解。」

當時,剛接觸python並理解如何在許多其他語言中執行該操作,我感到困惑的是沒有依賴命名節點來遍歷DOM的真實世界示例,這不是什麼我想要的。

希望這可以澄清問題,因爲此線程中的信息確實有用。

+0

你有試過什麼嗎?看看lxml。 – monkut

+0

另請參見:http://www.diveintopython.net/xml_processing/index.html#kgp.divein – Himanshu

+0

嗨,我不知道爲什麼這是封閉的「不是一個真正的問題」? 我問了一個非常具體的問題,並且對於試圖理解的概念非常精確。我的問題有什麼問題,以免我再犯同樣的錯誤? – Baywatch

回答

4

退房的ElementTree蟒的幫助文檔

的代碼從頁面的基本存根是:

import xml.etree.ElementTree as ET 
    tree = ET.parse(filename) 
    root = tree.getroot() 
    for child in root: 
     child.tag, child.attrib 

可以繼續運行for child in root:遞歸下降,直到沒有任何更多的孩子。

+0

謝謝!正是我在找什麼。 – Baywatch

3

use cElementTree; 它比ElementTree的Python版本快15-20倍,並且使用2-5倍的內存。 http://effbot.org/zone/celementtree.htm

import xml.etree.cElementTree as ET 
tree = ET.parse('test.xml') 
for elem in tree.getiterator(): 
    if elem.tag: 
     print 'my name:' 
     print '\t'+elem.tag 
    if elem.text: 
     print 'my text:' 
     print '\t'+(elem.text).strip() 
    if elem.attrib.items(): 
     print 'my attributes:' 
     for key, value in elem.attrib.items(): 
      print '\t'+'\t'+key +' : '+value 
    if list(elem): # use elem.getchildren() for python2.6 or before 
     print 'my no of child: %d'%len(list(elem)) 
    else: 
     print 'No child' 
    if elem.tail: 
     print 'my tail:' 
     print '\t'+'%s'%elem.tail.strip() 
    print '$$$$$$$$$$'