I had a problem with xml formatting when writing to an xml file. The problem is that the first time I write to an xml file, the xml is formatted correctly using pretty_print = True. Any subsequent attempts to add to the xml file are not formatted properly. Xml is written but not formatted. My code looks like this:
if os.path.isfile(libraryFile):
library = ET.ElementTree()
library.parse(libraryFile)
else:
library = ET.ElementTree(project.getBoilerplateLibrary(path))
root = library.getroot()
root.append(xml)
f = open(libraryFile, 'w')
library.write(f, pretty_print=True)
f.close()
The first time we write to a file, I get something like:
<root>
<element>
<foo>bar</foo>
</element>
</root>
Any subsequent attempts to add to this file are as follows:
<root>
<element>
<foo>bar</foo>
</element><element><bleep>bloop</bleep></element></root>
Any ideas?
source
share