I am trying to use elementTree to get at information in an xml response.
The response xmlresponse.xml
looks like:
<result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://somewhere.co.uk/">
<count>1</count>
<pageInformation>
<offset>0</offset>
<size>10</size>
</pageInformation>
<items>
<person uuid="1">
<name>
<firstName>John</firstName>
<lastName>Doe</lastName>
</name>
<ManagedByRelations>
<managedByRelation Id="1234">
<manager uuid="2">
<name formatted="false">
<text>Jane Doe</text>
</name>
</manager>
<managementPercentage>30</managementPercentage>
<period>
<startDate>2019-09-26</startDate>
</period>
</managedByRelation>
<managedByRelation Id="1234">
<manager uuid="3">
<name formatted="false">
<text>Joe Bloggs</text>
</name>
</manager>
<managementPercentage>70</managementPercentage>
<period>
<startDate>2019-09-26</startDate>
</period>
</managedByRelation>
</ManagedByRelations>
<fte>0.0</fte>
</person>
</items>
</result>
How do I get the information contained using elementTree, for example how can I retrieve the list of managers names, ids and start dates?
If I do:
from xml.etree.ElementTree import Element, ParseError, fromstring, tostring, parse
tree = parse('xmlresponse.xml')
root = tree.getroot()
for manager in root.findall('managedByRelation'):
print(manager)
The findall() doesnt return anything. I know i could do a list(root.iter())
to iterate through everything in the tree, but I want to know why root.findall()
isn't working as I expect?