Skip to content Skip to sidebar Skip to footer

How To Write Xml File With Multiple Root Element Using Elementtree In Python

I have python script and I have already written logic of writing xml file using xml.etree.cElementTree and the logic is look like below import xml.etree.cElementTree as ET root =

Solution 1:

What you want to generate is not valid xml. See Do you always have to have a root node with xml/xsd? for more info.

Also you can always manually concatenate the string.

import xml.etree.cElementTree as ET
result= ''for I inrange(0, 10):
    root = ET.Element("root")
    ET.SubElement(root, "field1").text = "some value1"
    ET.SubElement(root, "field2").text = "some vlaue2"
    result += ET.tostring(root)
print(result) # or write the result to a file

Post a Comment for "How To Write Xml File With Multiple Root Element Using Elementtree In Python"