2012-11-18 151 views
0

我想使用下面提供的python代碼來讀取xml文件email.xml(下面的數據),我不能夠打印XML文件中存在的實際數據,但獲得下面的輸出。我哪裏錯了?沒有得到xml文件輸出

電流輸出

xmlfile 
<open file 'email.xml', mode 'r' at 0x0226AF98> 
[<DOM Element: to at 0x231d620>] 
[<DOM Element: cc at 0x231d6c0>] 
[<DOM Element: bcc at 0x231d760>] 

Python代碼

import xml.dom.minidom as minidom 

def getemaildata(): 
    # Open the XML file 
    xmlfile = open('email.xml','r') 
    print "xmlfile" 
    print xmlfile 
    dom = minidom.parse(xmlfile) 
    email=dom.getElementsByTagName('email') 
    for node in email: 
     toemail=dom.getElementsByTagName('to') 
     print toemail 
     ccemail=dom.getElementsByTagName('cc') 
     print ccemail 
     bccemail=dom.getElementsByTagName('bcc') 
     print bccemail 
return (toemail,ccemail,bccemail) 

def main(): 
(To,CC,BCC)=getemaildata() 

if __name__ == '__main__': 
main() 

email.xml文件

<email> 
    <to>[email protected];[email protected]; 
     [email protected];[email protected];</to> 
    <cc> data.team </cc> 
    <bcc>[email protected]</bcc>  
</email> 

回答

2

您正在從XML解析器獲取「元素」對象的列表。您需要進一步迭代才能找到實際的「文本」節點。

例如:

# this returns a list of all Elements that have the tag "to" 
toemail=dom.getElementsByTagName('to') 

# Here we take the first node returned with tag 'to', then it's first child node 
textnode = toemail[0].childNodes[0] 

# print the data in the textnode 
print textnode.data 

要清潔從文本節點中的地址:

for address in textnode.data.split(';'): 
    if address == '': 
     # Catch empty entries as a result of trailing ; 
     continue 
    email = i.strip().strip('\n') 
    print email 
+0

thanks..how格式化data..I看是越來越打印的數據原樣不帶任何格式像http://pastie.org/5398584 – user1795998

+0

我試圖拆分和加入使用「;」但仍然相同tolist = textnode.data.split(';') print「;」。join(tolist) – user1795998

+0

XML將精確地存儲兩個標籤之間的內容,因此您可以確保您的輸入XML已被清理刪除空白和換行符),或對每個地址執行清理。我在回答中添加了一個編輯,以顯示如何完成這個感冒。理想情況下,您的電子郵件地址將分別存儲在自己的標記中,而不是捆綁在一起,但我猜測您的輸入XML數據來自電子郵件標題,因此您可能無法以更好的方式獲取數據。 – Shootfast