I've looked for many ways of  writing xml files like lxml, minidom, etc. However, none does what I want. Let me start from scratch. I chose to use xml.etree.ElementTree. Here is my code
from xml.etree.ElementTree import Element, SubElement, ElementTree, tostring
from xml.dom import minidom
def prettify(elem):
    """Return a pretty-printed XML string for the Element.
    """
    rough_string = tostring(elem, 'utf-8')
    reparsed = minidom.parseString(rough_string)
    return reparsed.toprettyxml(indent="  ")
Clip = Element('Clip')
tree = ElementTree(Clip)
Header = SubElement(Clip, 'Header')
Filename = SubElement(Header, 'Filename')
Filename.text = "C001101_001.mp4"
Duration = SubElement(Header, 'Duration')
Duration.text = "00:07:13"
Alarms =  SubElement(Clip, 'Alarms')
Alarm =  SubElement(Alarms, 'Alarm')
StartTime = SubElement(Alarm, "StartTime")
StartTime.text = "00:03:27"
AlarmDescription = SubElement(Alarm, "AlarmDescription")
AlarmDescription.text = "Loitering"
AlarmDuration = SubElement(Alarm, "AlarmDuration")
AlarmDuration.text = "00:00:44"
print(prettify(Clip))
tree.write(open("obbo.xml", "wb"))
print(prettify(Clip)) prints this to the console
<?xml version="1.0" ?>
<Clip>
  <Header>
    <Filename>C001101_001.mp4</Filename>
    <Duration>00:07:13</Duration>
  </Header>
  <Alarms>
    <Alarm>
      <StartTime>00:03:27</StartTime>
      <AlarmDescription>Loitering</AlarmDescription>
      <AlarmDuration>00:00:44</AlarmDuration>
    </Alarm>
  </Alarms>
</Clip>
What I want is to write obbo.xml file as exactly as the printed form, tree.write(open("obbo.xml", "wb")) does write, but it is in one line:
<Clip><Header><Filename>C001101_001.mp4</Filename><Duration>00:07:13</Duration></Header><Alarms><Alarm><StartTime>00:03:27</StartTime><AlarmDescription>Loitering</AlarmDescription><AlarmDuration>00:00:44</AlarmDuration></Alarm></Alarms></Clip>
Hope you can help~
