This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: ElementTree only writes declaration when passed encoding
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: ZackerySpytz, eli.bendersky, martin.panter, scoder, towb
Priority: normal Keywords:

Created on 2014-10-13 11:16 by towb, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg229238 - (view) Author: (towb) Date: 2014-10-13 11:16
This generates an XML declaration:

    import xml.etree.ElementTree as ET

    root = ET.Element('rss', version='2.0')
    tree = ET.ElementTree(root)
    tree.write('test.xml', encoding='iso-8859-1', xml_declaration=True)

However the declaration disappears if your don't pass an encoding. This doesn't match the documentation:

    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 or Unicode (default is None).
msg250347 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2015-09-10 02:28
As far as I can tell this was already working, even in 3.4.0a1 (the first pre-release of 3.4):

>>> tree.write('/dev/stdout', xml_declaration=True)
<?xml version='1.0' encoding='us-ascii'?>
<rss version="2.0" />

But it is not working in Python 2:

>>> tree.write('/dev/stdout', xml_declaration=True)
<rss version="2.0" />

Looking at the history, I am confused because a 3.2.3 revision (df8609f1854d) comes up in the history of 2.7’s /Doc/library/xml.etree.elementtree.rst. Perhaps it is just that the documentation has inherited some wording from Python 3. Possibly also relevant: revision 57e631f <https://bitbucket.org/mirror/cpython/commits/57e631f> and Issue 8047.
msg369178 - (view) Author: Zackery Spytz (ZackerySpytz) * (Python triager) Date: 2020-05-18 05:42
Python 2 is EOL, so I think this issue should be closed.
msg369184 - (view) Author: Stefan Behnel (scoder) * (Python committer) Date: 2020-05-18 06:17
Right, thanks. Closing since this works in Py3.
History
Date User Action Args
2022-04-11 14:58:09adminsetgithub: 66812
2020-05-18 06:17:28scodersetstatus: open -> closed
resolution: out of date
messages: + msg369184

stage: needs patch -> resolved
2020-05-18 05:42:17ZackerySpytzsetnosy: + ZackerySpytz
messages: + msg369178
2015-09-10 02:28:07martin.pantersetversions: + Python 2.7, - Python 3.4
nosy: + martin.panter

messages: + msg250347

stage: needs patch
2014-10-13 15:56:48scodersetnosy: + scoder, eli.bendersky
2014-10-13 11:16:08towbcreate