2016-12-06 123 views
0

我一直停留在這個問題上幾天的現在時:問題解析XML文件

我有一個XML文件,它是與此類似(同項100S)

<?xml version='1.0' encoding='us-ascii'?> 
<content> 
    <email a="1" b="0">[email protected]</email> 
    <email a="0" b="1">[email protected]</email> 
</content> 

我當前的代碼試圖通過這個XML文件解析:

from xml.dom import minidom 
    xmldoc = minidom.parse("data.xml") 
    content = xmldoc.getElementsByTagName("content") 
    address = xmldoc.getElementsByTagName("email") 
    for addresses in address: 
     Allow = True 
     Block = True 
     addressName = xmldoc.getElementsByTagName("email") 
     getB = addresses.attributes["b"] 
     b = getB.value 
     getA = addresses.attributes["a"] 
     a= getA.value 
    #setting allow and block list values 
     if (a == "1"): 
     Allow = True 
     print("This is allowed.") 
     elif (b == "1"): 
     Block = True 
     print("No, you cannot do that") 

現在,我得到了以下的輸出:

<DOM Element: addr at 0x3102850> 
This is allowed. 
<DOM Element: addr at 0x3102850> 
No, you cannot do that 

我的預期/希望的結果是:

[email protected] 
This is allowed. 
[email protected] 
No, you cannot do that 

如果任何人都可以在正確的方向指向我,那將是美好的。我仍然在編程初學者,我有點停留在那一刻。我也對不起,如果格式不正確,這是我第一次發佈。

謝謝!

回答

0

我猜你想在某個階段,你不顯示打印addressName。這是一個節點列表,所以你可以嘗試

print (addressName[0].firstChild.nodeValue) 

但是你已經有地址的節點,以便您可以只

print (addresses.firstChild.nodeValue) 

剝離下來:

from xml.dom import minidom 
xmldoc = minidom.parse("data.xml") 
address = xmldoc.getElementsByTagName("email") 
for addresses in address: 
    Allow = True 
    Block = True 
    b = addresses.attributes["b"].value 
    a = addresses.attributes["a"].value 
    #setting allow and block list values 
    print (addresses.firstChild.nodeValue) 
    if (a == "1"): 
    Allow = True 
    print("This is allowed.") 
    elif (b == "1"): 
    Block = True 
    print("No, you cannot do that") 

但它有可能你有一個以上的文本元素更多不同的XML,所以你可能需要使用:

print (" ".join(t.nodeValue for t in addresses.childNodes if t.nodeType == t.TEXT_NODE)) 

(另外你正在使用的時候,你應該使用的地址,反之亦然,但它不造成一個問題,只是難以閱讀地址)