Extracting values from XML using Python
I have the following xml extracted from another xml file.
<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>
I'm using the following Python code.
from xml.dom import minidom
xmldoc = minidom.parse('example.xml')
grammarNode = xmldoc.childNodes[2]
notificationsNode = grammarNode.childNodes[9]
print notificationsNode.toxml()
This python code gives the output of the xml which i have given above.
I tried the following to get the attribute values
notificationlist = xmldoc.getElementsByTagName('notification')
print notificationlist[0].toxml()
notification1 = notificationlist[0]
key = notification1.attributes.keys()
Using this I'm able to get only the values of the fist set of notification.
How is that i can get all the values of the attributes and store it in separate variables?
If you want to get the attributes for each item in notificationlist
, you could do this:
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
From here it would just be a matter of iterating this new list and pulling the attributes by name for each <notification>
element in notificationlist
.
for n in attrslist:
status = n['status']
oid = n['oid']
name = n['name']
# blah
Assuming that your 'notificationlist = xmldoc.getElementsByTagName('notification')' is generated from xmldoc which is the output value you listed you should have four elements. Thus just focusing on element 0 with notificationlist[0] will only address that first element. Here is some code with a modification to your samlpe xmldoc to make the discriptions differ by prepending aaa, bbb, ccc, ddd. You can capture the data by replacing the print statements---
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
---------------
精彩评论