[issue45336] Issue with xml.tree.ElementTree.write
New submission from ed wolf <bq824@cummins.com>: When executing the following command after modifiy an xml file an error is prodcued. import xml.etree.ElementTree as ET rtexmlFile = 'Fox_CM3550A_SWP1_Rte_ecuc.arxml' rte_ecu_tree = ET.parse(rtexmlFile) root = rte_ecu_tree.getroot() rte_ecu_tree.write(rtexmlFile, encoding="UTF-8", xml_declaration="True", default_namespace="None" method="xml",short_empty_elements="True" ) ValueError: cannot use non-qualified names with default_namespace option The documentation for the ElementTree.write function indicates the following format for this command but this format does not seem to wrok The write command does not also take into account when having standalone in the xml defintion. For ex, <?xml version="1.0" encoding="UTF-8" standalone="no"?> ElementTree.write will not add standalone back to the xml file Is this a bug in version 3.7? write(file, encoding="us-ascii", xml_declaration=None, default_namespace=None, method="xml", *, short_empty_elements=True) Writes the element tree to a file, as XML. file is a file name, or a file object opened for writing. encoding 1 is the output encoding (default is US-ASCII). 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). default_namespace sets the default XML namespace (for “xmlns”). method is either "xml", "html" or "text" (default is "xml"). The keyword-only short_empty_elements parameter controls the formatting of elements that contain no content. If True (the default), they are emitted as a single self-closed tag, otherwise they are emitted as a pair of start/end tags. The output is either a string (str) or binary (bytes). This is controlled by the encoding argument. If encoding is "unicode", the output is a string; otherwise, it’s binary. Note that this may conflict with the type of file if it’s an open file object; make sure you do not try to write a string to a binary stream and vice versa. ---------- assignee: docs@python components: Documentation messages: 402981 nosy: docs@python, twowolfs priority: normal severity: normal status: open title: Issue with xml.tree.ElementTree.write type: performance versions: Python 3.7 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue45336> _______________________________________
Andrei Kulakov <andrei.avk@gmail.com> added the comment: Ed: something looks a bit odd with the call to `write()` that you used:
rte_ecu_tree.write(rtexmlFile, encoding="UTF-8", xml_declaration="True", default_namespace="None" method="xml",short_empty_elements="True" )
Note that default val for `default_namespace` is None, but you have it quoted, and you also have quoted xml_declaration and short_empty_elements. Try re-running with these arg values unquoted. ---------- nosy: +andrei.avk _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue45336> _______________________________________
Change by Andrei Kulakov <andrei.avk@gmail.com>: ---------- type: performance -> behavior _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue45336> _______________________________________
ed wolf <bq824@cummins.com> added the comment: Hi Andrew I removed the quotes and still see an issue with the standalone not being added to the xml declaration. I set the command as follows rte_ecu_tree.write(rtexmlFile, encoding="UTF-8", xml_declaration=True, default_namespace=None, method="xml",short_empty_elements=True ) The xml declaration came out as <?xml version='1.0' encoding='UTF-8'?> but should be <?xml version="1.0" encoding="UTF-8" standalone="no"?> standalone did not get added to the declaration. ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue45336> _______________________________________
Andrei Kulakov <andrei.avk@gmail.com> added the comment: Ed: it seems ElementTree.write does not support `standalone` option, but both minidom (https://docs.python.org/3/library/xml.dom.minidom.html) and lxml (https://lxml.de/) support it. Are either of those suitable for your usecase? ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue45336> _______________________________________
Change by Andrei Kulakov <andrei.avk@gmail.com>: ---------- components: +Library (Lib) -Documentation nosy: +kj _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue45336> _______________________________________
Change by Andrei Kulakov <andrei.avk@gmail.com>: ---------- title: Issue with xml.tree.ElementTree.write -> xml.tree.ElementTree.write does not support `standalone` option _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue45336> _______________________________________
ed wolf <bq824@cummins.com> added the comment: Will ElementTree.write be updated to correct this issue? ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue45336> _______________________________________
Change by Andrei Kulakov <andrei.avk@gmail.com>: ---------- title: xml.tree.ElementTree.write does not support `standalone` option -> xml.etree.ElementTree.write does not support `standalone` option _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue45336> _______________________________________
Andrei Kulakov <andrei.avk@gmail.com> added the comment: Ed: I can look into adding it, but not sure when. If you can make the case that minidom and lxml are not suitable workarounds for this, it will be more likely that me or someone else will add this option; but note that as this is a new feature, it will only go into Python 3.11 . ---------- assignee: docs@python -> _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue45336> _______________________________________
Eric Vergnaud <eric.vergnaud@wanadoo.fr> added the comment: This is not a feature request, it's a bug fix request, so should be fixed asap. Why is it a bug ? XML spec says that "the default namespace does not apply to attribute names" (see section 6.3), therefore having a simple attribute name when using a default namespace is a perfectly valid scenario. Raising a ValueError in add_qname (line 864) is therefore incorrect if the qname being added is a simple name of an attribute not sure if lxml is able to parse very large documents (>4g) but I'll try it ---------- nosy: +ericvergnaud _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue45336> _______________________________________
Eric Vergnaud <eric.vergnaud@wanadoo.fr> added the comment: lxml tostring does not support the default_namespace value so not an option ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue45336> _______________________________________
Eric Vergnaud <eric.vergnaud@wanadoo.fr> added the comment: Actually there are 2 distinct issues here: - ValueError: cannot use non-qualified names with default_namespace option - lack of 'standalone' option when writing XML PI ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue45336> _______________________________________
Change by Ned Deily <nad@python.org>: ---------- nosy: +eli.bendersky, scoder _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue45336> _______________________________________
participants (4)
-
Andrei Kulakov
-
ed wolf
-
Eric Vergnaud
-
Ned Deily