("pilot_total_hours"str(incident pilot_total_hours))("midair"str(int(incident midair))))element setattribute(attributevaluefor nametext in (("airport"incident airport)("narrative"incident narrative))text_element tree createtextnode(textname_element tree createelement(namename_element appendchild(text_elementelement appendchild(name_elementroot appendchild(elementthe method begins by getting dom implementation by defaultthe implementation is provided by the expat xml parser the xml dom minidom module provides simpler and smaller dom implementation than that provided by the xml dom modulealthough the objects it uses are from the xml dom module once we have dom implementation we can create document the first argument to xml dom domimplementation createdocument(is the namespace uri which we don' needso we pass nonethe second argument is qualified name (the tag name for the root element)and the third argument is the document typeand again we pass none since we don' have document type having gotten the tree that represents the documentwe retrieve the root element and then proceed to iterate over all the incidents for each incident we create an elementand for each attribute we want the incident to have we call setattribute(with the attribute' name and value just as with the element treewe don' have to worry about escaping "&""(or about quotes in attribute valuesfor the airport and narrative text elements we must create text element to hold the text and normal element (with the appropriate tag nameas the text element' parent--we then add the normal element (and the text element it containsto the current incident element with the incident element completewe add it to the root fh none tryfh open(filename" "encoding="utf "tree writexml(fhencoding="utf- "return true xml encoding we have omitted the except and finally blocks since they are the same as ones we have already seen what this piece of code makes clear is the difference between the encoding string used for the built-in open(function and the encoding string used for xml filesas we discussed earlier importing an xml document into dom is similar to importing into an element treebut like exportingit requires more code we will look at the im