reading an xml file using an element tree is not much harder than writing one again there are two phasesfirst we read and parse the xml fileand then we traverse the resultant element tree to read off the data to populate the incidents dictionary again this second phase is not necessary if the element tree itself is being used as the in-memory data store here is the import_xml_etree(methodsplit into two parts def import_xml_etree(selffilename)trytree xml etree elementtree parse(filenameexcept (environmenterrorxml parsers expat expaterroras errprint("{ }import error{ }formatos path basename(sys argv[ ])err)return false by defaultthe element tree parser uses the expat xml parser under the hood which is why we must be ready to catch expat exceptions self clear(for element in tree findall("incident")trydata {for attribute in ("report_id""date""aircraft_id""aircraft_type""pilot_percent_hours_on_type""pilot_total_hours""midair")data[attributeelement get(attributedata["date"datetime datetime strptimedata["date"]"% -% -% "date(data["pilot_percent_hours_on_type"float(data["pilot_percent_hours_on_type"])data["pilot_total_hours"intdata["pilot_total_hours"]data["midair"bool(int(data["midair"])data["airport"element find("airport"text strip(narrative element find("narrative"text data["narrative"(narrative strip(if narrative is not none else ""incident incident(**dataself[incident report_idincident except (valueerrorlookuperrorincidenterroras errprint("{ }import error{ }formatos path basename(sys argv[ ])err)return false return true