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

时间:2016-06-08 17:24:36

标签: python python-2.7 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我也无法发现如何克服这一点。

提前致谢

1 个答案:

答案 0 :(得分:2)

试试这个:

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