2

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~

bit_scientist
  • 1,496
  • 2
  • 15
  • 34
  • Why not using `prettify` on the root element (well, actually you do already) and write the resulting string to file? – Michael Butscher Dec 27 '18 at 03:01
  • @MichaelButscher That's not an issue for now, I can optimize the code afterwards, thanks anyway – bit_scientist Dec 27 '18 at 04:03
  • Already answered here https://stackoverflow.com/questions/749796/pretty-printing-xml-in-python with a lot of options listed. In your case you may just save the output of `prettify(Clip)` to the file instead of `tree.write(...)`. –  Dec 27 '18 at 07:04
  • 1
    @Poolka, I've already seen that, they are about printing, not writing a new file as desired format. – bit_scientist Dec 27 '18 at 08:32
  • Read [print](https://docs.python.org/3/library/functions.html#print), how to use `file=...` – stovfl Dec 27 '18 at 08:55
  • 1
    @voo_doo Printing to console and writing to file are the same operation at the level you use `print` and `write` functions. The only difference is destination - console or file. In your code `prettify(Clip)` gives you pretty formatted string. Use it how you'd like - print to console or save to file. The linked question describes several ways to create pretty formatted string of xml data with Python. –  Dec 27 '18 at 10:23
  • Does this answer your question? [How do I get Python's ElementTree to pretty print to an XML file?](https://stackoverflow.com/questions/28813876/how-do-i-get-pythons-elementtree-to-pretty-print-to-an-xml-file) – Josh Correia Feb 12 '20 at 18:16

0 Answers0