thclpr
thclpr

Reputation: 5938

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

I'm not sure how i could get the values from <array name="logEntries" type="value" depth="1"> with the following code.

What i currently have so far, which works if have only one array tag on the xml, but not on multiple.

#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 sample:

<?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>

Desired output:

some inputs
other inputs

In short, even consulting The ElementTree XML API i'm unable to discover how can overcome this.

Thanks in advance

Upvotes: 0

Views: 102

Answers (1)

solbs
solbs

Reputation: 1068

Try this:

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

Upvotes: 2

Related Questions