1

I am trying to get information in an XML file. I am trying to do that with ElementTree, however it seems that what I am currently doing breaks the xml structure in python.

Here there is the XML:

<?xml version="1.0" encoding="UTF-8"?>
<TrainingCenterDatabase>
    <Activities>
        <Activity>
            <Id>2018-05-23</Id>
            <Lap>
                <TotalTimeSeconds>19204.0</TotalTimeSeconds>
                <MaximumHeartRateBpm>
                    <Value>137</Value>
                </MaximumHeartRateBpm>
                <Track>
                    <Trackpoint>
                        <Time>2018-05-23</Time>
                        <HeartRateBpm>
                            <Value>79</Value>
                        </HeartRateBpm>
                        <SensorState>Present</SensorState>
                    </Trackpoint>
                    <Trackpoint>
                        <Time>2018-05-23</Time>
                        <HeartRateBpm>
                            <Value>80</Value>
                        </HeartRateBpm>
                        <SensorState>Present</SensorState>
                    </Trackpoint>
                </Track>
            </Lap>            
        </Activity>
    </Activities>
</TrainingCenterDatabase>

My really simple code is the following:

e = ET.parse('fileName.xml').getroot()
e = e.find('./Activities/Activity/Lap/Track')    
print(e)

This code returns the following error:

AttributeError: 'NoneType' object has no attribute 'iter'

I have followed the documentation and some question on stack, as 1. My final output would be some variables that contain the one time information as: MaximumHeartRateBpm, TotalTimeSeconds and Id. Moreover I would like to get a list of dictionaries for the Track item with the following structure:

[{"Time": val, "HeartRateBpm": val, "SensorState": val},
...,
{"Time": val, "HeartRateBpm": val, "SensorState": val}]

I cannot attempt the second one as I am not able to look into the xml.

Thank you.

Guido Muscioni
  • 1,203
  • 3
  • 15
  • 37
  • What is the value of `e` after the execution of the _first_ statement? Is it a `None` by chance? Also, what is the value of `ET.parse('fileName.xml')`? Is _that one_ a `None` by chance? – DYZ May 23 '18 at 22:22
  • @DyZ If i print e after reading the xml I get this: "" – Guido Muscioni May 23 '18 at 22:23
  • 1
    I cannot reproduce your error. I get the printout `` on the last line of your snippet. – DYZ May 23 '18 at 22:28

0 Answers0