XML 属性用 python 和 ElementTree 解析

XML attribute parsing with python and ElementTree

伙计们! 我正在尝试解析一些奇怪的 XML:

<?xml version="1.0" encoding="UTF-8"?>
<analytics>
  <standard1>
    ...
    <attributes>
      <attribute name="agentname" value="userx userx" />
      <attribute name="agentpk" value="5" />
      <attribute name="analytics:callid" value="757004000003597" />
      ...
      <attribute name="wrapuptime" value="0" />
    </attributes>
  </standard1>
  <standard2>
    ...
    <attributes>
      <attribute name="agentname" value="userx userx" />
      <attribute name="agentpk" value="5" />
      <attribute name="analytics:callid" value="757004000003597" />
      ...
      <attribute name="wrapuptime" value="0" />
    </attributes>
  </standard2>
  <engines>
  ...
  </engines>
</analytics>

由于 namevalue 都是属性,我不知道如何访问 value 通过 name 而不用 foreach 循环遍历整个属性子部分。

知道如何使用 ElementTree 直接访问吗?

您可以使用简单的 XPath 表达式 通过 name 属性的值过滤 attribute 元素。示例工作代码:

import xml.etree.ElementTree as ET

data = """<?xml version="1.0" encoding="UTF-8"?>
<analytics>
  <standard>
      <attributes>
        <attribute name="agentname" value="userx userx" />
        <attribute name="agentpk" value="5" />
        <attribute name="analytics:callid" value="757004000003597" />
        <attribute name="wrapuptime" value="0" />
      </attributes>
  </standard>
</analytics>
"""

root = ET.fromstring(data)
print(root.find(".//attribute[@name='agentname']").attrib["value"])

打印:

userx userx

注意 xml.etree.ElementTree 有一个 limited XPath support