2013-03-09 118 views
0

XML =XML文件解析並獲取標籤?

<company>Mcd</company>   
<Author>Dr.D</Author> 

我想獲取MCD和Dr.D.
我嘗試

import xml.etree.ElementTree as et 
e = et.parse(xml) 
root = e.getroot() 
for node in root.getiterator("company"): 
    print node.tag 

跳頻了慷慨的幫助。

+0

嘿了我的答案:) 使用'xml.dom.minidom進口parseString – vivs 2013-03-09 11:31:13

+0

'xml.dom.minidom'不是推薦當然,除非你已經知道了DOM API密切。該文檔正確地建議您改用'ElementTree'。 – 2013-03-09 11:53:48

回答

0

只需找到相匹配的一個標籤,然後採取.text屬性:

company = root.find('.//company').text 
author = root.find('.//Author').text 
0

試試這個。

from xml.etree import ElementTree as ET 
    xmlFile = ET.iterparse(open('some_file.xml','r')) 

    for tag, value in xmlFile: 
     print value.text