0

I am using element tree with python to perse xml file. I am only inserting new nodes after a certain node. I am not modifying any attributes. The logic works fine and the new nodes get added to the proper places. But the in the output the positions of the attributes of the nodes are getting interchanged as shown below:

Input

<set title="Set Testcase1 to 128">
  <testvariable name="Testcase1" msg="TEST" node="TESTER" testtype="METHOD1">128</testvariable>
</set>
<match **title="match" timeout="220"**>
  <testvariable name="Testcase1" msg="TEST" node="TESTER" **testtype="METHOD1"**>128</testvariable>
</match>

Output

<set title="Set Testcase1 to 128">
  <testvariable testtype="METHOD1" msg="TEST" name="Testcase1" node="TESTER">128</testvariable>
</set>
<match timeout="220" title="match">
  <testvariable testtype="METHOD1" msg="TEST" name="Testcase1" node="TESTER">128</testvariable>
</match>

The write to output is done using: tree.write(r'textOut.xml',encoding="iso-8859-1").

Anyone please let me know why the attributes place get interchanged.

Thanking you in advance.

Rahul
  • 502
  • 7
  • 16
Biswal
  • 53
  • 7
  • The order of attributes in XML is by definition not significant (see https://www.w3.org/TR/REC-xml/#sec-starttags). You cannot make assumptions about a particular ordering. – mzjn Jun 27 '16 at 10:45
  • There are workarounds. For example: http://stackoverflow.com/questions/2741480/can-elementtree-be-told-to-preserve-the-order-of-attributes – mzjn Jun 27 '16 at 10:46
  • @mzjn : I have already seen the solution and still hoping for a better solution from ET itself. And for me the order matters inorder to have very minimal differences between the input and output. Thanks. – Biswal Jun 27 '16 at 11:35

0 Answers0