Minidom write XML removing empty node

249 Views Asked by At

I have xml file like this Input XML:

<India>
    <state>
        <name>Karnataka</name>
        <description>One State Many Worlds</description>
        <population></population>
    </state>

    <state>
        <name>Kerala</name>
        <description> Gods own country</description>
        <population></population>
    </state>

</India>

and, I process the data(process code/data not included for simplicity).

code:

import xml.dom.minidom 
from xml.dom.minidom import Node 
doc = xml.dom.minidom.parse("india.xml") 
doc.writexml(open('NewData.xml', 'w'))
doc.unlink()

Here is the output XML:

<?xml version="1.0" ?><India>
    <state>
        <name>Karnataka</name>
        <description>One State Many Worlds</description>
        <population/>
    </state>

    <state>
        <name>Kerala</name>
        <description> Gods own country</description>
        <population/>
    </state>

</India>

As you can see node is "half removed" in the out file. Need help to tackle this issue. thanks!

0

There are 0 best solutions below