使用 Python 从 XML 中提取值
我从另一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想获取
notificationlist
中每个项目的属性,您可以这样做:从这里开始,只需迭代这个新列表并按每个
< 的名称提取属性即可
元素。notificationlist
中的;notification>If you want to get the attributes for each item in
notificationlist
, you could do this:From here it would just be a matter of iterating this new list and pulling the attributes by name for each
<notification>
element innotificationlist
.假设您的 'notificationlist = xmldoc.getElementsByTagName('notification')' 是从 xmldoc 生成的,它是您列出的输出值,您应该有四个元素。因此,仅使用 notificationlist[0] 关注元素 0 将仅处理第一个元素。下面是一些对 samlpe xmldoc 进行修改的代码,通过在前面添加 aaa、bbb、ccc、ddd 来使说明有所不同。您可以通过替换 print 语句来捕获数据---
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---