*elem* is an element tree or an individual element.
+ .. versionchanged:: 3.8
+ The :func:`dump` function now preserves the attribute order specified
+ by the user.
+
.. function:: fromstring(text)
.. versionadded:: 3.4
The *short_empty_elements* parameter.
+ .. versionchanged:: 3.8
+ The :meth:`write` method now preserves the attribute order specified
+ by the user.
+
This is the XML file that is going to be manipulated::
# For this purpose, the module-level "ET" symbol is temporarily
# monkey-patched when running the "test_xml_etree_c" test suite.
+import contextlib
import copy
import functools
import html
method='html')
self.assertEqual(serialized, expected)
+ def test_dump_attribute_order(self):
+ # See BPO 34160
+ e = ET.Element('cirriculum', status='public', company='example')
+ with support.captured_stdout() as stdout:
+ ET.dump(e)
+ self.assertEqual(stdout.getvalue(),
+ '<cirriculum status="public" company="example" />\n')
+
+ def test_tree_write_attribute_order(self):
+ # See BPO 34160
+ root = ET.Element('cirriculum', status='public', company='example')
+ tree = ET.ElementTree(root)
+ f = io.BytesIO()
+ with contextlib.redirect_stdout(f):
+ tree.write(f, encoding='utf-8', xml_declaration=True)
+ self.assertEqual(f.getvalue(),
+ b"<?xml version='1.0' encoding='utf-8'?>\n"
+ b'<cirriculum status="public" company="example" />')
+
class XMLPullParserTest(unittest.TestCase):