Reputation: 347
I use this code to insert a new node in the original .xml (file taken from the ElementTree documentation) :
import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
my_xml = """
<country name="Togo">
<rank updated="yes">2</rank>
<year>2000</year>
<gdppc>3</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
"""
new_contry = ET.fromstring(my_xml)
root.insert(1,new_contry)
tree.write('essai_insert_output.xml')
root = tree.getroot()
But the result destroys the right indentation between the new node and the next one :
<data>
<country name="Liechtenstein">
<rank>1</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E" />
<neighbor name="Switzerland" direction="W" />
</country>
<country name="Togo">
<rank updated="yes">2</rank>
<year>2000</year>
<gdppc>3</gdppc>
<neighbor name="Austria" direction="E" />
<neighbor name="Switzerland" direction="W" />
</country><country name="Singapore">
<rank>4</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N" />
</country>
<country name="Panama">
<rank>68</rank>
<year>2011</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W" />
<neighbor name="Colombia" direction="E" />
</country>
</data>
The new node <country name="Togo">
is well placed but at the end of it, we have this collision :
</country><country name="Singapore">
How to cure this effect?
Upvotes: 0
Views: 2085
Reputation: 616
As of Python 3.9, a new method named indent() has been added to xml.etree.ElementTree
, which can be used to generate pretty-printed XML output without using another module or library.
To fix your example in Python 3.9+, just add the following line right before writing the tree to the file:
ET.indent(tree, ' ')
Upvotes: 1
Reputation: 2469
Format output.
from xml.dom import minidom
dom = minidom.parse('country_data.xml')
with open('dom_write.xml', 'w', encoding='UTF-8') as fh:
dom.writexml(fh, indent='', addindent='\t', newl='', encoding='UTF-8')
Upvotes: 0