使用Python从XML中提取值

时间:2011-04-20 11:02:14

标签: python xml xml-parsing

我从另一个xml文件中提取了以下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()

使用此功能,我只能获得第一组通知的值。

我如何获得属性的所有值并将其存储在单独的变量中?

2 个答案:

答案 0 :(得分: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

从这里开始,只需迭代这个新列表,并按<notification>中每个notificationlist元素的名称拉出属性。

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

答案 1 :(得分:0)

假设您的'notificationlist = xmldoc.getElementsByTagName('notification')'是从xmldoc生成的,这是您列出的输出值,您应该有四个元素。因此,仅关注具有notificationlist [0]的元素0将仅解决该第一元素。这里有一些代码修改了你的samlpe xmldoc,通过预先添加aaa,bbb,ccc,ddd来使描述有所不同。您可以通过替换print语句来捕获数据---

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
---------------