I wanted to add Element with Subelements to my xml file using Python. But after changing file the Element and Subelement looks like a line, not like a xml-tree.
I do this:
tree = ET.parse('existing_file.xml')
root = tree.getroot()
new_object = ET.SubElement(root, 'object')
name = ET.SubElement(new_object, 'name')
name.text = 'car'
color = ET.SubElement(new_object, 'color')
color.text = 'Red'
new_tree = ET.ElementTree(root)
new_tree.write('new_file.xml')
But after this I've got file without structure like this:
<object><name>car</name><color>red</color></object>
But i need this:
<object>
<name>car</name>
<color>red</color>
</object>
what do I do wrong?
indent(). See stackoverflow.com/a/39482716/407651