data["airport"get_text(airport childnodesnarrative element getelementsbytagname"narrative")[ data["narrative"get_text(narrative childnodesincident incident(**dataself[incident report_idincident except (valueerrorlookuperrorincidenterroras errprint("{ }import error{ }formatos path basename(sys argv[ ])err)return false return true once the dom exists we clear the current incidents data and iterate over all the incident tags for each one we extract the attributesand for datenumericand booleans we convert them to the correct types in exactly the same way as we did when using an element tree the only really significant difference between using dom and an element tree is in the handling of text nodes we use the xml dom element getelementsbytagname(method to get the child elements with the given tag name--in the cases of and we know there is always one of eachso we take the first (and onlychild element of each type then we use the nested get_text(function to iterate over these tagschild nodes to extract their texts as usualif any error occurs we catch the relevant exceptionprint an error message for the userand return false the differences in approach between dom and element tree are not greatand since they both use the same expat parser under the hoodthey're both reasonably fast manually writing xml |writing preexisting element tree or dom as an xml document can be done with single method call but if our data is not already in one of these forms we must create an element tree or dom firstin which case it may be more convenient to simply write out our data directly when writing xml files we must make sure that we properly escape text and attribute valuesand that we write well-formed xml document here is the export_xml_manual(method for writing out the incidents in xmldef export_xml_manual(selffilename)fh none tryfh open(filename" "encoding="utf "fh write('\ 'fh write("\ "local functions