2011-04-20 106 views
2

我有從另一個xml文件中提取的以下xml。使用Python從XML中提取值

<notifications> 
    <notification name="ccmSmtp" oid="1.3.6.1" status="current"> 
    <objects> 
     <object module="callhome" name="ccmSmtp" /> 
    </objects> 
    <description>This is a description</description> 
    </notification> 
    <notification name="ccmAlertGroup" oid="1.3.6.1" status="current"> 
    <objects> 
     <object module="callhome" name="callHome" /> 
    </objects> 
    <description>This is a description</description> 
    </notification> 
    <notification name="ccmAlert" oid="1.3.6.1" status="current"> 
    <objects> 
     <object module="callhome" name="callHome" /> 
    </objects> 
    <description>This is a description</description> 
    </notification> 
    <notification name="ccmSmtp" oid="1.3.6.1" status="current"> 
    <objects></objects> 
    <description>This is a description</description> 
    </notification> 
</notifications> 

我正在使用以下Python代碼。

from xml.dom import minidom 

xmldoc = minidom.parse('example.xml') 
grammarNode = xmldoc.childNodes[2] 
notificationsNode = grammarNode.childNodes[9] 
print notificationsNode.toxml() 

這個python代碼給出了我上面給出的xml的輸出。

我嘗試以下,以獲得屬性值

notificationlist = xmldoc.getElementsByTagName('notification') 
print notificationlist[0].toxml() 
notification1 = notificationlist[0] 
key = notification1.attributes.keys() 

使用這個我能得到拳頭組通知的唯一值。

我是如何獲得屬性的所有值並將其存儲在單獨的變量中?

+0

我不熟悉minidom,但我猜想你只是第一次得到第一個條目,因爲你只是要求通知列表中的第一個條目。爲什麼不設置一個foreach? '在通知名單中的項目:...' – emragins 2011-04-20 15:37:19

回答

0

假設您的'notificationlist = xmldoc.getElementsByTagName('notification')'是從您列出的輸出值xmldoc生成的,您應該有四個元素。因此,只關注通知列表[0]的元素0將只處理第一個元素。這裏是一些代碼,修改了你的samlpe xmldoc,通過添加aaa,bbb,ccc,ddd來區別不同。您可以通過更換打印語句捕獲數據---

for x in notificationlist: 
    print '*' * 15 
    print x.getElementsByTagName('description').item(0).childNodes[0].data 
    print 
    for y in x.attributes.keys(): 
    print y 
    print x.attributes.getNamedItem(y).nodeValue 
    print '-' *15 



*************** 
     aaaThis is a description 

status 
current 
--------------- 
oid 
1.3.6.1 
--------------- 
name 
ccmSmtp 
--------------- 
*************** 
     bbbThis is a description 

status 
current 
--------------- 
oid 
1.3.6.1 
--------------- 
name 
ccmAlertGroup 
--------------- 
*************** 
    cccThis is a description 

status 
current 
--------------- 
oid 
1.3.6.1 
--------------- 
name 
ccmAlert 
--------------- 
*************** 
     dddThis is a description 

status 
current 
--------------- 
oid 
1.3.6.1 
--------------- 
name 
ccmSmtp 
--------------- 
+0

抱歉包裹... – zedman9991 2011-04-20 18:58:55

+0

哇那裏,可能想要修復格式。 – jathanism 2011-04-20 18:58:57

+0

好點。只是想出瞭如何做到這一點。 – zedman9991 2011-04-21 18:40:45

1

如果你想獲得每個項目的屬性在notificationlist,你可以這樣做:

attrslist = [dict(node.attributes.items()) for node in notificationlist] 
print attrslist[0] 
# => {u'status': u'current', u'oid': u'1.3.6.1', u'name': u'ccmSmtp'} 
print attrslist[0]['status'] 
# => current 

從這裏它只是可以迭代這個新列表,並按notificationlist中的每個<notification>元素的名稱拉取屬性。

for n in attrslist: 
    status = n['status'] 
    oid = n['oid'] 
    name = n['name'] 
    # blah