xml.etree.ElementTree.tostring writes a XML encoding declaration with encoding='utf8'
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
lxml |
Won't Fix
|
Undecided
|
Unassigned |
Bug Description
The docstring for tostring states this:
```
Serialize an element to an encoded string representation of its XML
tree.
Defaults to ASCII encoding without XML declaration. This
behaviour can be configured with the keyword arguments 'encoding'
(string) and 'xml_declaration' (bool). Note that changing the
encoding to a non UTF-8 compatible encoding will enable a
declaration by default.
```
And the code is this:
```
elif xml_declaration is None:
# by default, write an XML declaration only for non-standard encodings
```
I'm not familiar enough with lxml to say what goes wrong, but it should not write a XML declaration for `encoding='utf8'`.
However, it does with the current version from PyPI.
This
```
from xml.etree import ElementTree as ET
ele = ET.Element('test')
print(ET.
print(ET.
```
produces the following output:
```
b"<?xml version='1.0' encoding=
b'<test />'
```
Python : sys.version_
lxml.etree : (3, 4, 4, 0)
libxml used : (2, 9, 2)
libxml compiled : (2, 9, 2)
libxslt used : (1, 1, 28)
libxslt compiled : (1, 1, 28)
You seem to be confusing xml.etree (stdlib) and lxml.etree (external package) here.
It's open for debate if lxml should behave the same as ElementTree here, but since the behaviour has been like this essentially forever, and users can easily request the declaration via an option, it's probably ok to leave it as it is.