我有以下xml文件,来自网址
<queue name="triggerPO.EPOEPOImportEBDToPO-Start">
<stats size="0" consumerCount="10" enqueueCount="21" dequeueCount="21"/>
<feed>
<atom>
queueBrowse/triggerPO.EPOEPOImportEBDToPO-Start?view=rss&feedType=atom_1.0
</atom>
<rss>
queueBrowse/triggerPO.EPOEPOImportEBDToPO-Start?view=rss&feedType=rss_2.0
</rss>
</feed>
</queue>
<queue name="hibernateSearchQueue">
<stats size="0" consumerCount="1" enqueueCount="41648" dequeueCount="41648"/>
<feed>
<atom>
queueBrowse/hibernateSearchQueue?view=rss&feedType=atom_1.0
</atom>
<rss>
queueBrowse/hibernateSearchQueue?view=rss&feedType=rss_2.0
</rss>
</feed>
</queue>
<queue name="POEPOExportSearchRequestToEPO-XML-IPX">
<stats size="0" consumerCount="10" enqueueCount="0" dequeueCount="0"/>
<feed>
<atom>
queueBrowse/POEPOExportSearchRequestToEPO-XML-IPX?view=rss&feedType=atom_1.0
</atom>
<rss>
queueBrowse/POEPOExportSearchRequestToEPO-XML-IPX?view=rss&feedType=rss_2.0
</rss>
</feed>
</queue>
如何才能获得“hibernateSearchQueue”属性的“统计信息大小值”(例如“0”)。这应该在Python中实现
答案 0 :(得分:0)
我建议您使用{{3}}:
from lxml import etree
您的XML示例格式不正确,因此我添加了<root>
元素:
content = """\
<root>
<queue name="triggerPO.EPOEPOImportEBDToPO-Start">
<stats size="0" consumerCount="10" enqueueCount="21" dequeueCount="21"/>
<feed>
<atom>
queueBrowse/triggerPO.EPOEPOImportEBDToPO-Start?view=rss&feedType=atom_1.0
</atom>
<rss>
queueBrowse/triggerPO.EPOEPOImportEBDToPO-Start?view=rss&feedType=rss_2.0
</rss>
</feed>
</queue>
<queue name="hibernateSearchQueue">
<stats size="0" consumerCount="1" enqueueCount="41648" dequeueCount="41648"/>
<feed>
<atom>
queueBrowse/hibernateSearchQueue?view=rss&feedType=atom_1.0
</atom>
<rss>
queueBrowse/hibernateSearchQueue?view=rss&feedType=rss_2.0
</rss>
</feed>
</queue>
<queue name="POEPOExportSearchRequestToEPO-XML-IPX">
<stats size="0" consumerCount="10" enqueueCount="0" dequeueCount="0"/>
<feed>
<atom>
queueBrowse/POEPOExportSearchRequestToEPO-XML-IPX?view=rss&feedType=atom_1.0
</atom>
<rss>
queueBrowse/POEPOExportSearchRequestToEPO-XML-IPX?view=rss&feedType=rss_2.0
</rss>
</feed>
</queue>
</root>
"""
要解析XML文件,可以使用etree.parse()
,但是,在这里,我使用etree.XML()
来解析字符串。
tree = etree.XML(content)
要获取“hibernateSearchQueue”队列,可以使用XPath表达式:
queues = tree.xpath("//queue[@name = 'hibernateSearchQueue']")
queue = queues[0]
etree.dump(queue)
你会得到:
<queue name="hibernateSearchQueue">
<stats size="0" consumerCount="1" enqueueCount="41648" dequeueCount="41648"/>
<feed>
<atom>
queueBrowse/hibernateSearchQueue?view=rss&feedType=atom_1.0
</atom>
<rss>
queueBrowse/hibernateSearchQueue?view=rss&feedType=rss_2.0
</rss>
</feed>
</queue>
要获取统计信息的大小值,您可以执行以下操作:
for stats in queue.iter("stats"):
print(stats.attrib["size"])
# 0