Why does xml package modify my xml file in Python3?

Note that “xml package” and “the xml library” are ambiguous. There are several XML-related modules in the standard library: https://docs.python.org/3/library/xml.html.

Why is it modified?

ElementTree moves namespace declarations to the root element, and namespaces that aren’t actually used in the document are removed.

Why does ElementTree do this? I don’t know, but perhaps it is a way to make the implementation simpler.

How can I prevent this? e.g. I just want to replace specific tag or it’s value in a quite complex xml-file without loosing any other informations.

I don’t think there is a way to prevent this. The issue has been brought up before. Here are two very similar questions with no answers:

My suggestion is to use lxml instead of ElementTree. With lxml, the namespace declarations will remain where they occur in the original file.

Line 1 is gone.

That line is the XML declaration. It is recommended but not mandatory to have one.

If you always want an XML declaration, use xml_declaration=True in the write() method call.

Leave a Comment