code: def _write_pretty_result_xml_to_file(self, xml, filename):
          
          if self.compress_results:
              actual_filename = filename + ".bz2"
              # Use BZ2File directly or our hack for Python 3.2
              open_func = bz2.BZ2File if hasattr(bz2.BZ2File, 'writable') else util.BZ2FileHack
          else:
              # write content to temp file first to prevent loosing data
              # in existing file if writing fails
              actual_filename = filename + ".tmp"
              open_func = open
  
          with io.TextIOWrapper(open_func(actual_filename, 'wb'), encoding='utf-8') as file:
              rough_string = ET.tostring(xml, encoding='unicode')
              reparsed = minidom.parseString(rough_string)
              doctype = minidom.DOMImplementation().createDocumentType(
                      'result', RESULT_XML_PUBLIC_ID, RESULT_XML_SYSTEM_ID)
              reparsed.insertBefore(doctype, reparsed.documentElement)
              reparsed.writexml(file, indent="", addindent="  ", newl="\n", encoding="utf-8")
  
          if self.compress_results:
              # try to delete uncompressed file (would have been overwritten in no-compress-mode)
              try:
                  os.remove(filename)
              except OSError:
                  pass
              self.all_created_files.discard(filename)
              self.all_created_files.add(actual_filename)
          else:
              os.rename(actual_filename, filename)
              self.all_created_files.add(filename)
  
          return filename
docs: Writes a nicely formatted XML file with DOCTYPE, and compressed if necessary.