I’m writing some XML with element tree.
I’m giving the code an empty template file that starts with the XML declaration:<?xml version= "1.0"?> when ET has finished making its changes and writes the completed XML its stripping out the declarion and starting with the root tag. How can I stop this?
Write call:
ET.ElementTree(root).write(noteFile)
According to the documentation:
So,
write(noteFile)is explicitly telling it to write an XML declaration only if the encoding is not US-ASCII or UTF-8, and that the encoding is US-ASCII; therefore, you get no declaration.I’m guessing if you didn’t read this much, your next question is going to be "why is my Unicode broken", so let’s fix both at once: