我有從另一個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()
使用這個我能得到拳頭組通知的唯一值。
我是如何獲得屬性的所有值並將其存儲在單獨的變量中?
我不熟悉minidom,但我猜想你只是第一次得到第一個條目,因爲你只是要求通知列表中的第一個條目。爲什麼不設置一個foreach? '在通知名單中的項目:...' – emragins 2011-04-20 15:37:19