I have two xml templates: annotation.xml and object.xml.
<annotation>
<filename>test.jpg</filename>
<source>
<annotation>test.py</annotation>
</source>
<size>
<width>256</width>
<height>256</height>
<depth>3</depth>
</size>
</annotation>
<object>
<name>1</name>
<bndbox>
<xmin>0</xmin>
<ymin>0</ymin>
<xmax>1</xmax>
<ymax>1</ymax>
</bndbox>
</object>
I am reading these two templates using lxml
and building an output xml file that includes one or several objects inside the annotation. Here is a minimal working example:
from lxml import etree
# parse xml templates
annotation = etree.parse('annotation.xml').getroot()
obj = etree.parse('object.xml').getroot()
# append object to annotation
annotation.append(obj)
# save output file
tree = etree.ElementTree(annotation)
tree.write('test.xml', encoding='utf-8', xml_declaration=True)
Although this works, the result I am getting does not have a proper format and looks something like this:
<?xml version='1.0' encoding='UTF-8'?>
<annotation>
<filename>test.jpg</filename>
<source>
<annotation>test.py</annotation>
</source>
<size>
<width>256</width>
<height>256</height>
<depth>3</depth>
</size>
<object>
<name>1</name>
<bndbox>
<xmin>0</xmin>
<ymin>0</ymin>
<xmax>1</xmax>
<ymax>1</ymax>
</bndbox>
</object></annotation>
whereas I would expect the following formatted file:
<?xml version='1.0' encoding='UTF-8'?>
<annotation>
<filename>test.jpg</filename>
<source>
<annotation>test.py</annotation>
</source>
<size>
<width>256</width>
<height>256</height>
<depth>3</depth>
</size>
<object>
<name>1</name>
<bndbox>
<xmin>0</xmin>
<ymin>0</ymin>
<xmax>1</xmax>
<ymax>1</ymax>
</bndbox>
</object>
</annotation>
I've also tried using etree.tostring(tree, pretty_print=True)
and then saving the file but had no luck and the file looks the same.