file handling once we have the element tree we can iterate over every using the xml etree elementtree findall(method each incident is returned as an xml etree element object we use the same technique for handling the element attributes as we did in the previous section' import_text_regex(method--first we store all the values in the data dictionaryand then we convert those values which are datesnumbersor booleans to the correct type for the airport and narrative subelements we use the xml etree element find(method to find them and read their text attributes if text element has no text its text attribute will be noneso we must account for this when reading the narrative text element since it might be empty in all casesthe attribute values and text returned to us do not contain xml escapes since they are automatically unescaped as with all the xml parsers used to process aircraft incident dataan exception will occur if the aircraft or narrative element is missingor if one of the attributes is missingor if one of the conversions failsor if any of the numeric data is out of range--this ensures that invalid data will cause parsing to stop and for an error message to be output the code at the end for creating and storing incidents and for handling exceptions is the same as we have seen before dom (document object model|the dom is standard api for representing and manipulating an xml document in memory the code for creating dom and writing it to fileand for parsing an xml file using domis structurally very similar to the element tree codeonly slightly longer we will begin by reviewing the export_xml_dom(method in two parts this method works in two phasesfirst dom is created to reflect the incident dataand then the dom is written out to file just as with an element treesome programs might use the dom as their data structurein which case they can simply write out the data def export_xml_dom(selffilename)dom xml dom minidom getdomimplementation(tree dom createdocument(none"incidents"noneroot tree documentelement for incident in self values()element tree createelement("incident"for attributevalue in ("report_id"incident report_id)("date"incident date isoformat())("aircraft_id"incident aircraft_id)("aircraft_type"incident aircraft_type)("pilot_percent_hours_on_type"str(incident pilot_percent_hours_on_type))