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>If you want create xml you may just do this:</p>

<pre><code>from lxml import etree
try:
   root_text = raw_input("Enter the root Element: \n")
   root = etree.Element(root_text)
   child_tag = raw_input("Enter the child tag Element: \n")
   child_text = raw_input("Enter the child text Element: \n")
   child = etree.Element(child_text )
   child.text =child_text 
   root.append(child)
   with open('file.xml', 'w') as f:
      f.write(etree.tostring(root))
      f.close()
except ValueError:
    print("Occured Error")
</code></pre>

<p>Or if you want dynamic length you just use for loop.</p>

python xml