【问题标题】:Fetching the values with Element Tree使用元素树获取值
【发布时间】:2013-02-14 10:15:29
【问题描述】:

我想获取特定组件的一些值。例如,我想从以下输出中仅获取 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"/>

【问题讨论】:

    标签: python elementtree xml.etree


    【解决方案1】:

    您需要在 XPath 查询中包含完整的命名空间:

    for component in tree.iterfind('{http://www.rejex.com/statistics}Component'):
        print component.attrib['name']
    

    或者,您可以使用显式命名空间映射,将前缀(您选择)映射到命名空间 URI:

    nsmap = {'rej': 'http://www.rejex.com/statistics`}
    
    for stat in tree.iterfind('rej:Stat', namespaces=nsmap):
        print stat.attrib['value']
    

    rej 前缀会在您作为 namespaces 传入的任何内容中查找,然后转换为第一个示例中给出的相同 XPath 查询。

    您可以扩展 {namespace} xpath 限定符以查找更复杂的匹配项:

    tree.find(
        "{http://www.rejex.com/statistics}Component[@name='paristrain']/"
        "{http://www.rejex.com/statistics}Stat[@name='TimeoutValue']")
    

    例如,应该返回具有name="TimeoutValue" 属性的Stat 元素,其父元素是具有name="paristrain" 属性的Component 元素。

    【讨论】:

    • 事情是在我的 xml 文件中有这么多的组件。我只对获取组件 paristrain 及其 TimeoutValue: Value 的值感兴趣。
    • @fear_matrix:您可以使用命名空间前缀为其创建 XPath 表达式。
    • @fear_matrix:添加了未经测试的示例 XPath 表达式。您没有包含任何示例 XML,因此很难为您测试。
    • 我已经提到了上面的xml文件
    • @fear_matrix:您离开了 xmlns 命名空间。在这种情况下,这些很重要。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-11
    • 1970-01-01
    • 2014-08-09
    • 1970-01-01
    • 2014-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多