Python - Element Tree is removing the XML declaration
Asked Answered
D

2

13

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)

Dunstan answered 27/9, 2012 at 1:6 Comment(0)
A
26

According to the documentation:

write(file, encoding="us-ascii", xml_declaration=None, method="xml")

Writes the element tree to a file, as XML. file is a file name, or a file object opened for writing. encoding 1 is the output encoding (default is US-ASCII). xml_declaration controls if an XML declaration should be added to the file. Use False for never, True for always, None for only if not US-ASCII or UTF-8 (default is None). method is either "xml", "html" or "text" (default is "xml"). Returns an encoded string.

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:

ET.ElementTree(root).write(noteFile, encoding="utf-8", xml_declaration=True)
Archean answered 27/9, 2012 at 1:18 Comment(3)
Ah! OK, I didn't realise it was completely reforming the document - thank you, that's very useful to know.Dunstan
Well, it's up to the ElementTree implementation to decide whether to generate the entire XML from scratch or to reuse existing pieces. I believe lxml has some pretty smart caching, so if you just read in a small file and write it back out, it reuses the entire root node. But the declaration isn't part of the root node, so there's really no way it could get around that.Archean
If above accepted solution don't work for you, please check the answer of @Olli as his little tweak saved my dayDibbrun
D
6

There are different versions of ElementTree. Some of them accept the xml_declaration argument, some do not.

The one I happen to have does not. It emits the declaration if and only if encoding != 'utf-8'. So, to get the declaration, I call write(filename, encoding='UTF-8').

Dzoba answered 2/11, 2013 at 3:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.