I have an ElementTree
object in Python 3.5.
I need to write it into a file with few requirements:
- With indentation (pretty print of 2 spaces).
- Without the version line.
- Empty tags should appear with both opening and closing tags (for example
<sometag>
</sometag>
and not
<sometag/>
)
EDIT
Okay, so far I have this code
pp_xml = xml.dom.minidom.parseString(ET.tostring(self._root)).\
toprettyxml(indent=' ')
# Remove the version line
pp_xml = pp_xml[pp_xml.find('\n')+1:]
ET.ElementTree(ET.fromstring(pp_xml)).write(self._out_filepath,
short_empty_elements=False)
Which does almost everything I asked for, except that that empty tags appear like this:
<sometag></sometag>
And I need them to be in new lines like this:
<sometag>
</sometag>