0

When creating an xml file using python, is it possible to change the order of the attributes of the nodes?

root = ET.Element("testsuites")
suite = ET.SubElement(
    root,
    "testsuite",
    {
        "name": "ABC",
        "Suit_id": "1234",
        "Suit_total_tests": "12",
        "Suit_failures": "2",
        "Suit_log": "Log",
    },
)
xmlstr = minidom.parseString(ET.tostring(root)).toprettyxml(indent="   ")
with open("Test.xml", "w", encoding='utf-8') as f:
    f.write(xmlstr)

I get

<testsuite Suit_failures="0" Suit_id="12" Suit_log="Log" Suit_total_tests="12" name="ABC">

Desired Output:-

<testsuite name="ABC" Suit_id="12" Suit_log="Log" Suit_total_tests="12" >

I read about this and see that the order does not matter or one should not be expecting such ordering. In other cases I see it is possible using Etree or Ordered Keys. However, I couldn't figure out how to apply that here. Some clarity would help. Thanks.

Abhishek Rai
  • 2,159
  • 3
  • 18
  • 38

0 Answers0