diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py
index ae06a9cc11855f..b5a22e8c216212 100644
--- a/Lib/test/test_xml_etree.py
+++ b/Lib/test/test_xml_etree.py
@@ -4021,6 +4021,54 @@ def test_write_to_user_binary_writer_with_bom(self):
'''\n'''
''''''.encode("utf-16"))
+ def test_custom_declaration_to_user_binary_writer_with_bom(self):
+ tree = ET.ElementTree(ET.XML(''''''))
+ raw = io.BytesIO()
+ writer = self.dummy()
+ writer.write = raw.write
+ writer.seekable = lambda: True
+ writer.tell = raw.tell
+ tree.write(writer, encoding="utf-16", xml_declaration_definition='')
+ self.assertEqual(raw.getvalue(),
+ '''\n'''
+ ''''''.encode("utf-16"))
+
+ def test_custom_declaration2_to_user_binary_writer_with_bom(self):
+ tree = ET.ElementTree(ET.XML(''''''))
+ raw = io.BytesIO()
+ writer = self.dummy()
+ writer.write = raw.write
+ writer.seekable = lambda: True
+ writer.tell = raw.tell
+ tree.write(writer, encoding="utf-16", xml_declaration_definition='')
+ self.assertEqual(raw.getvalue(),
+ '''\n'''
+ ''''''.encode("utf-16"))
+
+ def test_custom_declaration3_to_user_binary_writer_with_bom(self):
+ tree = ET.ElementTree(ET.XML(''''''))
+ raw = io.BytesIO()
+ writer = self.dummy()
+ writer.write = raw.write
+ writer.seekable = lambda: True
+ writer.tell = raw.tell
+ tree.write(writer, encoding="utf-16", xml_declaration_definition='')
+ self.assertEqual(raw.getvalue(),
+ '''\n'''
+ ''''''.encode("utf-16"))
+
+ def test_custom_declaration4_to_user_binary_writer_with_bom(self):
+ tree = ET.ElementTree(ET.XML(''''''))
+ raw = io.BytesIO()
+ writer = self.dummy()
+ writer.write = raw.write
+ writer.seekable = lambda: True
+ writer.tell = raw.tell
+ tree.write(writer, encoding="utf-16", xml_declaration_definition='')
+ self.assertEqual(raw.getvalue(),
+ '''\n'''
+ ''''''.encode("utf-16"))
+
def test_tostringlist_invariant(self):
root = ET.fromstring('foo')
self.assertEqual(
diff --git a/Lib/xml/etree/ElementTree.py b/Lib/xml/etree/ElementTree.py
index ce67d7d7d54748..06d5f04ac577ed 100644
--- a/Lib/xml/etree/ElementTree.py
+++ b/Lib/xml/etree/ElementTree.py
@@ -681,7 +681,8 @@ def write(self, file_or_filename,
xml_declaration=None,
default_namespace=None,
method=None, *,
- short_empty_elements=True):
+ short_empty_elements=True,
+ xml_declaration_definition=""):
"""Write element tree to a file as XML.
Arguments:
@@ -694,6 +695,14 @@ def write(self, file_or_filename,
is added if encoding IS NOT either of:
US-ASCII, UTF-8, or Unicode
+ *xml_declaration_definition* -- string for customizing encoding declaration
+ as documentation always shows doublequotes but
+ singlequote where hardcoded here.
+ to be rfc conform which allows doublequotes and
+ singlequote for declaration. default value is
+ untouched to pass tests.
+ placeholders: {version}, {encoding}
+
*default_namespace* -- sets the default XML namespace (for "xmlns")
*method* -- either "xml" (default), "html, "text", or "c14n"
@@ -719,8 +728,10 @@ def write(self, file_or_filename,
(xml_declaration is None and
encoding.lower() != "unicode" and
declared_encoding.lower() not in ("utf-8", "us-ascii"))):
- write("\n" % (
- declared_encoding,))
+ # version configuration is'nt necessary, can be overwritten
+ # in declaration_definition at runtime
+ data = {'version':'1.0', 'encoding': declared_encoding}
+ write(xml_declaration_definition.format(**data)+"\n")
if method == "text":
_serialize_text(write, self._root)
else:
diff --git a/Misc/NEWS.d/next/Library/2024-12-20-18-54-53.gh-issue-128136.tgbvWt.rst b/Misc/NEWS.d/next/Library/2024-12-20-18-54-53.gh-issue-128136.tgbvWt.rst
new file mode 100644
index 00000000000000..f2886bd4bbf1ae
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-12-20-18-54-53.gh-issue-128136.tgbvWt.rst
@@ -0,0 +1,8 @@
+New parameter added to the write function that allows you to customize the xml declaration, which was previously hard-coded.
+The standard functionality is retained; if you don't specify the xml_declaration_definition parameter, the previous standard declaration is used. ````
+When specifying the parameter, the following options are available.
+E.g.:
+tree.write(file, encoding="utf-8", xml_declaration=True, xml_declaration_definition='''''')
+tree.write(file, encoding="utf-8", xml_declaration=True, xml_declaration_definition='''''')
+tree.write(file, encoding="utf-8", xml_declaration=True, xml_declaration_definition='''''')
+The placeholders {version} and {encoding} are replaced, version is always 1.0 and enconding depends on the code as before.