I'm trying to convert my xml-request (see example below) into a pandas-dataframe, but it doesn't work the way it should and I'm not sure why.
Example xml-request
<workingTimes>
<day>
<date>2015-09-21</date>
<dayOfWeek>Mon</dayOfWeek>
<employee>
<firstName>Albert</firstName>
<lastName>Grimaldi</lastName>
<login xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<personnelNumber xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<duration>00:00:00</duration>
<rest mandatory="00:00:00">00:00:00</rest>
<costCenter>AB-1234</costCenter>
</employee>
<employee>
<firstName>Max</firstName>
<lastName>Mustermann</lastName>
<login xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<personnelNumber>12346</personnelNumber>
<duration>00:00:00</duration>
<rest mandatory="00:00:00">00:00:00</rest>
<costCenter>AB-1234</costCenter>
</employee>
</day>
<day>
<date>2015-09-22</date>
<dayOfWeek>Tue</dayOfWeek>
<employee>
<firstName>Albert</firstName>
<lastName>Grimaldi</lastName>
<login xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<personnelNumber xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<duration>00:00:00</duration>
<rest mandatory="00:00:00">00:00:00</rest>
<costCenter>AB-1234</costCenter>
</employee>
<employee>
<firstName>Max</firstName>
<lastName>Mustermann</lastName>
<login xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<personnelNumber>12346</personnelNumber>
<duration>00:00:00</duration>
<rest mandatory="00:00:00">00:00:00</rest>
<costCenter>AB-1234</costCenter>
</employee>
</day>
</workingTimes>
Code:
import pandas as pd
from xml.etree import ElementTree as et
...
r = requests.get(api_url, headers=headers)
root = et.fromstring(r.content)
df_cols, rows = ['date', 'dayOfWeek', 'firstName', 'lastName', 'duration', 'costCenter'], []
for child in root:
s_date = child.attrib.get("date")
s_dayOfWeek = child.attrib.get("dayOfWeek")
s_firstName = child.find("firstName").text if child is not None else None
s_lastName = child.find("lastName").text if child is not None else None
s_duration= child.find("duration").duration if child is not None else None
s_costCenter= child.find("costCenter").text if child is not None else None
rows.append({'date': s_date, 'dayOfWeek': s_dayOfWeek, 'firstName': s_firstName, 'lastName':
s_lastName, 'duration': s_duration, 's_costCenter': costCenter})
df_xml = pd.DataFrame(rows, columns=df_cols)
And this is a part of the documentary:
Can anyone tell me what I'm doing wrong?
dateanddayOfWeekare not at the same level as the rest if the properties you are looking for.