我想获取特定组件的一些值。例如,我想从以下输出中仅获取 2 个值(即 Component --> name: paristrain 和 Stat --> TimeoutValue: value)。我试图用 xpath 做到这一点,但我无法获得所需的输出。你能帮我解决这个问题吗?
from xml.etree import ElementTree
with open('rejexstats.xml', 'rt') as f:
tree = ElementTree.parse(f)
for node in tree.iter():
print node.tag, node.attrib
这打印:
Statistics {}
{http://www.rejex.com/stats}Server {'start': '2013-01-22T22:30:13.583', 'product': 'rejex', 'end': '2013-01-23T09:39:45.249', 'startup': '2013-01-22T22:30:13.583', 'name': 'localhost'}
{http://www.rejex.com/statistics}Component {'subtype': 'Thread', 'type': 'Supplier', 'name': 'paristrain'}
{http://www.rejex.com/statistics}Stat {'type': 'entry', 'name': 'TimeoutValue', 'value': '120'}
{http://www.rejex.com/statistics}Stat {'type': 'entry', 'name': 'PendingRequests', 'value': '0'}
{http://www.rejex.com/statistics}Stat {'type': 'entry', 'name': 'Session|0|SupplierTimeout', 'value': '0'}
{http://www.rejex.com/statistics}Stat {'type': 'entry', 'name': 'Session|0|Errors', 'value': '0'}
{http://www.rejex.com/statistics}Stat {'type': 'entry', 'name': 'Session|3|SupplierTimeout', 'value': '0'}
{http://www.rejex.com/statistics}Stat {'type': 'entry', 'name': 'ApplyRulesErrors', 'value': '0'}
XML 文件
<Statistics>
<Server end="2013-02-14T07:06:35.533" name="localhost" product="regex" start="2013-02-13T22:30:12.982" startup="2013-02-13T22:30:12.982">
<Component name="paristrain" subtype="Thread" type="Supplier">
<Stat name="TimeoutValue" type="entry" value="120"/>
<Stat name="PendingRequests" type="entry" value="0"/>
<Stat name="Session|0|SupplierTimeout" type="entry" value="0"/>
<Stat name="Session|0|Errors" type="entry" value="0"/>
<Stat name="Session|3|SupplierTimeout" type="entry" value="0"/>
<Stat name="ApplyRulesErrors" type="entry" value="0"/>
<Stat name="LateResponses" type="entry" value="0"/>
<Stat name="CacheTries" type="entry" value="0"/>
<Stat name="Session|4|Errors" type="entry" value="0"/>
<Stat name="MaxActiveThreads" type="entry" value="0"/>
<Stat name="MaxPendingQueueSize" type="entry" value="10"/>
<Stat name="ValidResponses" type="entry" value="0"/>
<Stat name="TranslateResponses" type="entry" value="0"/>