How to create xml in Python dynamically?
<p>import xml.etree.ElementTree as ET</p>

<pre><code> var3 = raw_input("Enter the root Element: \n")
 root = ET.Element(var3) 
 var4 = raw_input("Enter the sub root Element: \n")
 doc = ET.SubElement(root, var4)   

 no_of_rows=input("Enter the number of Element for XML files: -    \n")

 def printme():

    var = raw_input("Enter Element: - \n")
    var1 = raw_input("Enter Data: - \n")

    ET.SubElement(doc, var).text =var1

    return;

    for num in range(0, no_of_rows):
        printme()

    tree = ET.ElementTree(root)
    file = raw_input("Enter File Name: - \n")
    tree.write(file)

    ET.ElementTree(root).write(file, encoding="utf-8", xml_declaration=True)         
    print "Xml file Created..!!"
</code></pre>

<p>Above one the script that i want create xml dynamically using python. Just i need that it creates only one sub root. How to create multiple  sub root with elements in it. What i done wrong in above code ?</p>

<p>You are taking no. of elements from user but you are not using it.
Use a loop and get element details from user within the loop as shown below: </p>

<pre><code>import xml.etree.ElementTree as ET
try:
    no_of_rows=int(input("Enter the number of Element for XML files: -    \n"))
    root = input("Enter the root Element: \n")
    root_element = ET.Element(root)
    for _ in range(1, no_of_rows):
        tag = input("Enter Element: - \n")
        value = input("Enter Data: - \n")    
        ET.SubElement(root_element, tag).text = value

    tree = ET.ElementTree(root_element)
    tree.write("filename.xml")

    print("Xml file Created..!!")
except ValueError:
    print("Value Error")
except:
    print("Exception Occuured")
    enter code here
</code></pre>

<p>I hope this is what you want to achieve.</p>

python xml