1

I have been researching a way to parse an xml document with more than one root in python but have been unsuccessful. Does anyone know of any helpful sites to accomplish this or have any isight as to whether this can be done? I have the xml file and python code below. I get a 'NoneType' object has no attribute 'text' error in my read loop of my python code.

XML File:

<ThursdayDay12>

<event>
<title>Refrigerator/freezer</title>
<startHour>11</startHour>
<startMinute>00</startMinute>
<duration units = 'min'>780</duration>
<load units = 'W'>33.77</load>
<comment>
'HANOVER HANRT30C Model'
</comment> 
</event>

<event>
<title>Temperature</title>
<startHour>7</startHour>
<startMinute>30</startMinute>
<duration units = 'min'>990</duration>
<load units = 'W'>3520</load>
<comment>
'Assume AC requirement for house is 1 TR=3.52 kW'
</comment>
</event>

<event>
<title>Indoor lighting</title>
<startHour>20</startHour>
<startMinute>00</startMinute>
<duration units = 'min'>240</duration>
<load units = 'W'>250</load>
<comment>
'LED lighting for 4 rooms'
</comment>
</event>

</ThursdayDay12>


<FridayDay13>

<event>
<title>TV</title>
<startHour>19</startHour>
<startMinute>30</startMinute>
<duration units = 'min'>150</duration>
<load units = 'W'>3.96</load>
<comment>
'VIZIO E28h-C1 model rated at 34.7 kWh/yr'
</comment>
</event>

<event>
<title>Heat water for showers</title>
<startHour>19</startHour>
<startMinute>30</startMinute>
<duration units = 'min'>150</duration>
<load units = 'W'>1385</load>
<comment></comment> 
</event>

</FridayDay13>

</SD2017NominalEnergyUse>

Python Code:

import xml.etree.ElementTree as ET

tree = ET.parse('SD2017NominalEnergyUse.xml')
root = tree.getroot()


title, start, end, load, duration = [],[],[],[],[]

for child in root:
   for grandchild in child:

        title.append(child.find('title').text)

        sh = int(child.find('startHour').text)
        sm = int(child.find('startMinute').text)
        duration.append(float(child.find('duration').text))
        start.append(sh*60+sm)
        end.append(start[-1] + duration[-1])
        load.append(float(child.find('load').text))

P = 0.0
for i in range(len(root)):
    P = P+load[i]

print(P)
Zach Thomas
  • 147
  • 12

1 Answers1

1

Have you tried xml.dom or minidom?

Frangipanes
  • 380
  • 2
  • 14
  • I have not found that helpful in helping me parse an xml file with children and grandchildren attributes – Zach Thomas Aug 02 '16 at 16:11
  • @ZachThomas [How do I parse XML in Python](http://stackoverflow.com/questions/1912434/how-do-i-parse-xml-in-python) – Frangipanes Aug 02 '16 at 16:17
  • yes but it does not appear to address how to parse xml files with grandchildren parts to the file. the parse in etree does not work with grandchildren attributes of an xml file – Zach Thomas Aug 03 '16 at 02:04
  • How does it not? Read the documentation: `for child in root: #do something`. If you want to go deeper just loop through again: `for child in root: for child in root: #do something` – Frangipanes Aug 03 '16 at 10:53
  • Using an "extra for child in root:" does not allow me to access my grandchildren part of the xml document. There is only one root in an xml document and so calling "for child in root:" twice does not do anything. – Zach Thomas Aug 03 '16 at 16:23
  • Sorry, `for children in root: for grandchild in children: #do something` – Frangipanes Aug 03 '16 at 16:24
  • I tried that but I get an error in my loop saying 'NoneType' object has no attribute 'text'. I have updated my question to include my actual xml and python codes – Zach Thomas Aug 03 '16 at 16:42
  • Nevermind, it worked. Thanks! I just had to change the child part in my append calls to grandchild. – Zach Thomas Aug 03 '16 at 16:46