For the :class:`Document` node, an additional keyword argument *encoding* can
be used to specify the encoding field of the XML header.
+ .. versionchanged:: 3.8
+ The :meth:`writexml` method now preserves the attribute order specified
+ by the user.
.. method:: Node.toxml(encoding=None)
encoding. Encoding this string in an encoding other than UTF-8 is
likely incorrect, since UTF-8 is the default encoding of XML.
+ .. versionchanged:: 3.8
+ The :meth:`toxml` method now preserves the attribute order specified
+ by the user.
+
.. method:: Node.toprettyxml(indent="", newl="", encoding="")
Return a pretty-printed version of the document. *indent* specifies the
The *encoding* argument behaves like the corresponding argument of
:meth:`toxml`.
+ .. versionchanged:: 3.8
+ The :meth:`toprettyxml` method now preserves the attribute order specified
+ by the user.
+
.. _dom-example:
import copy
import pickle
+import io
+import contextlib
from test.support import findfile
import unittest
pi = doc.createProcessingInstruction("y", "z")
pi.nodeValue = "crash"
+ def test_minidom_attribute_order(self):
+ xml_str = '<?xml version="1.0" ?><curriculum status="public" company="example"/>'
+ doc = parseString(xml_str)
+ output = io.StringIO()
+ doc.writexml(output)
+ self.assertEqual(output.getvalue(), xml_str)
+
+ def test_toxml_with_attributes_ordered(self):
+ xml_str = '<?xml version="1.0" ?><curriculum status="public" company="example"/>'
+ doc = parseString(xml_str)
+ self.assertEqual(doc.toxml(), xml_str)
+
+ def test_toprettyxml_with_attributes_ordered(self):
+ xml_str = '<?xml version="1.0" ?><curriculum status="public" company="example"/>'
+ doc = parseString(xml_str)
+ self.assertEqual(doc.toprettyxml(),
+ '<?xml version="1.0" ?>\n'
+ '<curriculum status="public" company="example"/>\n')
+
if __name__ == "__main__":
unittest.main()
writer.write(indent+"<" + self.tagName)
attrs = self._get_attributes()
- a_names = sorted(attrs.keys())
- for a_name in a_names:
+ for a_name in attrs.keys():
writer.write(" %s=\"" % a_name)
_write_data(writer, attrs[a_name].value)
writer.write("\"")
-ElementTree now preserves the attribute order specified by the user.
+ElementTree and minidom now preserve the attribute order specified by the user.