Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to xml add a new element to a new line

Executing the code:

parser = ET.XMLParser(strip_cdata=False, )
tree = ET.parse(f'copy_{xml_file}', parser)
root[0].insert(0, ET.Element("type"))
root.write("test.xml", pretty_print=True)

And the element I added creates not on a new line but in front of another element, it turns out the form:

    <firstTeg>327</firstTeg>
    <secondTeg>1.0</secondTeg><type/>
    

I need to get this kind of:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    <firstTeg>327</firstTeg>
    <secondTeg>1.0</secondTeg>
    <type/>

How do I create a new tag on a new line?

>Solution :

Add

ET.indent(tree, '  ')

line before

root.write("test.xml", pretty_print=True)

Note: Feature available from Python 3.9

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading