使用 Python 从 XML 中提取值

发布于 2024-11-02 05:03:56 字数 1622 浏览 0 评论 0原文

我从另一个 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()

使用此方法,我只能获取第一组通知的值。

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮萍、无处依 2024-11-09 05:03:56

如果您想获取 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

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
━╋う一瞬間旳綻放 2024-11-09 05:03:56

假设您的 '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
---------------

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
---------------
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文