Skip to content

Commit 1f00810

Browse files
authored
Update ElementTree.py
Added possibility to customize hardcoded xml declaration
1 parent dbd08fb commit 1f00810

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

Lib/xml/etree/ElementTree.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,7 @@ def iterfind(self, path, namespaces=None):
679679
def write(self, file_or_filename,
680680
encoding=None,
681681
xml_declaration=None,
682+
xml_declaration_definition='<?xml version="{version}" encoding="{encoding}"?>',
682683
default_namespace=None,
683684
method=None, *,
684685
short_empty_elements=True):
@@ -694,6 +695,14 @@ def write(self, file_or_filename,
694695
is added if encoding IS NOT either of:
695696
US-ASCII, UTF-8, or Unicode
696697
698+
*xml_declaration_definition* -- string for customizing encoding declaration
699+
as documentation always shows doublequotes but
700+
singlequote where hardcoded here.
701+
to be rfc conform which allows doublequotes and
702+
singlequote for declaration. default value is
703+
switched to doublquotes here to stay on one format.
704+
placeholders: {version}, {encoding}
705+
697706
*default_namespace* -- sets the default XML namespace (for "xmlns")
698707
699708
*method* -- either "xml" (default), "html, "text", or "c14n"
@@ -719,8 +728,10 @@ def write(self, file_or_filename,
719728
(xml_declaration is None and
720729
encoding.lower() != "unicode" and
721730
declared_encoding.lower() not in ("utf-8", "us-ascii"))):
722-
write("<?xml version='1.0' encoding='%s'?>\n" % (
723-
declared_encoding,))
731+
# version configuration is'nt necessary, can be overwritten
732+
# in declaration_definition at runtime
733+
data = {'version':'1.0', 'encoding': declared_encoding}
734+
write(xml_declaration_definition.format(**data)+"\n")
724735
if method == "text":
725736
_serialize_text(write, self._root)
726737
else:

0 commit comments

Comments
 (0)