使用 xml.etree.elementTree 从 xml 解析属性名称文本

Parsing attribute name text from xml using xml.etree.elementTree

我不确定如何使用以下代码从 <array name="logEntries" type="value" depth="1"> 获取值。

我目前拥有的,如果在 xml 上只有 一个 数组标记,但在多个上没有。

#Currently xml_input var is returned from an http request
root = ElementTree.fromstring(xml_input)
for child in root.findall('.array/value'):
    print(child)

XML样本:

<?xml version="1.0" encoding="UTF-8"?>
<Values version="2.0">
  <array name="logList" type="value" depth="1">
    <value>type_log</value>
  </array>
  <value name="numlines">2</value>
  <array name="numlinesList" type="value" depth="1">
    <value>2</value>
  </array>
  <array name="logEntries" type="value" depth="1">
    <value>some inputs</value>
    <value>other inputs</value>
  </array>
</Values>

期望的输出:

some inputs
other inputs

简而言之,即使咨询The ElementTree XML API我也无法发现如何克服这个问题。

提前致谢

试试这个:

for child in root.findall('.//array[@name="logEntries"]/value'):
    print(child.text)