Python XML modifying by ElementTree destroys the XML structure
<p>I am using Python V 3.5.1 on windows framework in order to modify a text inside , the modification works great but after saving the tree all the empty tags get destroyed as the following example:</p>

<pre><code>&lt;HOSTNAME&gt;&lt;/HOSTNAME&gt; Is being changed to &lt;HOSTNAME /&gt;
</code></pre>

<p>child with a text between the tags looks good:</p>

<pre><code>&lt;HOSTNAME&gt;tnas2&lt;/HOSTNAME&gt; is being changed to 
&lt;HOSTNAME&gt;tnas2&lt;/HOSTNAME&gt; which is the same as the source.
</code></pre>

<p>The source XML file is:</p>

<pre><code>&lt;ROOT&gt;

    &lt;DeletedName&gt;

        &lt;VERIFY_DEST_SIZE&gt;Y&lt;/VERIFY_DEST_SIZE&gt;

        &lt;VERIFY_BYTES&gt;Y&lt;/VERIFY_BYTES&gt;

        &lt;TIMESTAMP&gt;XXXXXXXXXDeletedXXXXXXXXXX&lt;/TIMESTAMP&gt;

        &lt;EM_USERS&gt;XXXXXXXXXDeletedXXXXXXXXXX&lt;/EM_USERS&gt;

        &lt;EM_GROUPS&gt;&lt;/EM_GROUPS&gt;

        &lt;LOCAL&gt;

            &lt;HOSTNAME&gt;&lt;/HOSTNAME&gt;

            &lt;PORT&gt;&lt;/PORT&gt;

            &lt;USERNAME&gt;XXXXXXXXXDeletedXXXXXXXXXX&lt;/USERNAME&gt;

            &lt;PASSWORD&gt;XXXXXXXXXDeletedXXXXXXXXXX&lt;/PASSWORD&gt;

            &lt;HOME_DIR&gt;&lt;/HOME_DIR&gt;

            &lt;OS_TYPE&gt;Windows&lt;/OS_TYPE&gt;

        &lt;/LOCAL&gt;

        &lt;REMOTE&gt;

            &lt;HOSTNAME&gt;DeletedHostName&lt;/HOSTNAME&gt;

            &lt;PORT&gt;22&lt;/PORT&gt;

            &lt;USERNAME&gt;XXXXXXXXXDeletedXXXXXXXXXX&lt;/USERNAME&gt;

            &lt;PASSWORD&gt;XXXXXXXXXDeletedXXXXXXXXXX&lt;/PASSWORD&gt;

            &lt;HOME_DIR&gt;XXXXXXXXXDeletedXXXXXXXXXX&lt;/HOME_DIR&gt;

            &lt;OS_TYPE&gt;Unix&lt;/OS_TYPE&gt;

            &lt;CHAR_SET&gt;UTF-8&lt;/CHAR_SET&gt;

            &lt;SFTP&gt;Y&lt;/SFTP&gt;

            &lt;ENCRYPTION&gt;Blowfish&lt;/ENCRYPTION&gt;

            &lt;COMPRESSION&gt;N&lt;/COMPRESSION&gt;

        &lt;/REMOTE&gt;

    &lt;/DeletedName&gt;

&lt;/ROOT&gt;
</code></pre>

<p>the code is:</p>

<pre><code>import os
import xml.etree.ElementTree as ET
from shutil import copyfile
import datetime

def AddAuthUserToAccountsFile(AccountsFile,RemoteMachine,UserToAdd):
    today = datetime.date.today()
    today = str(today)
    print(today)
    BackUpAccountsFile = AccountsFile + "-" + today
    try:
        tree = ET.parse(AccountsFile)
    except:
        pass
    try:
        copyfile(AccountsFile,BackUpAccountsFile)
    except:
        pass
    root = tree.getroot()
    UsersTags = tree.findall('.//EM_USERS')
    for UsersList in UsersTags:
        Users = UsersList.text
        Users = UsersList.text = Users.replace("||","|")
        if UserToAdd not in Users:
            print("The Users were : ",Users, "---&gt;&gt; Adding ",UserToAdd)
            UsersList.text = Users + UserToAdd +"|"
    tree.write(AccountsFile)
</code></pre>

<p>Appreciate for any help to pass this strange scenario.</p>

<p>Thanks,
Miki</p>

<p>OK, i found the solution  - 
just adding method = "html" to the tree.write line it keeps it as needed.</p>

<pre><code>tree.write(AccountsFile,method = 'html')
</code></pre>

<p>Thanks.</p>

python xml elementtree