Is there a way to remove xml header added by ElementTree.tostring method?



Before writing processed data to xml I do some formatting so it would all look nice in the result xml document.



import xml.etree.ElementTree as et
import xml.dom.minidom as mdom
(...)

for i in range(10):
root = et.Element("main")
(...)

ugly_xml = et.tostring(root, 'utf-8', method='xml')
parsed_xml = mdom.parseString(ugly_xml)
nice_xml = parsed_xml.toprettyxml(indent=" " * 3)
with open('test.xml', 'a') as f:
f.write(nice_xml)


However the result file obviously has duplicate xml headers.



<?xml version="1.0" ?>
(...)
<?xml version="1.0" ?>
(...)
<?xml version="1.0" ?>


Is there a way not to print xml header with tostring method? The docs didn't provide any info except that I can just try different types like 'html' or 'text'.


No comments:

Post a Comment