title: Generating XML dynamically in Python
body: <p>Hey friends I am generating XML data using Python libraries as follow </p>
  
  <pre class="lang-py prettyprint-override"><code>def multiwan_info_save(request):
      data = {}
      init = "init"
      try:
         form = Addmultiwanform(request.POST)
      except:
          pass
      if form.is_valid():
          from_sv = form.save(commit=False)
          obj_get = False
          try:
              obj_get = MultiWAN.objects.get(isp_name=from_sv.isp_name)
          except:
              obj_get = False
              nameservr  = request.POST.getlist('nameserver_mw') 
              for nm in nameservr:
                  nameserver1, is_new = NameServer.objects.get_or_create(name=nm)
                  from_sv.nameserver = nameserver1
                  from_sv.save()
          #    main(init)    
          top = Element('ispinfo')
         # comment = Comment('Generated for PyMOTW')
          #top.append(comment)
          all_connection = MultiWAN.objects.all()
          for conn in all_connection:
              child = SubElement(top, 'connection number ='+str(conn.id)+'name='+conn.isp_name+'desc='+conn.description )
              subchild_ip = SubElement(child,'ip_address')
              subchild_subnt = SubElement(child,'subnet')
              subchild_gtwy = SubElement(child,'gateway')
              subchild_nm1 = SubElement(child,'probe_server1')
              subchild_nm2 = SubElement(child,'probe_server2')
              subchild_interface = SubElement(child,'interface')
              subchild_weight = SubElement(child,'weight')
              subchild_ip.text = str(conn.ip_address)
              subchild_subnt.text = str(conn.subnet)
              subchild_gtwy.text = str(conn.gateway)
              subchild_nm1.text = str(conn.nameserver.name)
             # subchild_nm2.text = conn.
              subchild_weight.text = str(conn.weight)
              subchild_interface.text = str(conn.interface)
          print "trying to print _____________________________"
          print tostring(top)   
          print "let seeeeeeeeeeeeeeeeee +++++++++++++++++++++++++"
  </code></pre>
  
  <p>But I am getting output like follow </p>
  
  <pre><code>&lt;ispinfo&gt;&lt;connection number =5name=Airtelllldesc=Largets TRelecome &gt;&lt;ip_address&gt;192.168.1.23&lt;/ip_address&gt;&lt;subnet&gt;192.168.1.23&lt;/subnet&gt;&lt;gateway&gt;192.168.1.23&lt;/gateway&gt;&lt;probe_server1&gt;192.168.99.1&lt;/probe_server1&gt;&lt;probe_server2 /&gt;&lt;interface&gt;eth0&lt;/interface&gt;&lt;weight&gt;160&lt;/weight&gt;&lt;/connection number =5name=Airtelllldesc=Largets TRelecome &gt;&lt;connection number =6name=Uninordesc=Uninor&gt;&lt;ip_address&gt;192.166.55.23&lt;/ip_address&gt;&lt;subnet&gt;192.166.55.23&lt;/subnet&gt;&lt;gateway&gt;192.168.1.23&lt;/gateway&gt;&lt;probe_server1&gt;192.168.99.1&lt;/probe_server1&gt;&lt;probe_server2 /&gt;&lt;interface&gt;eth0&lt;/interface&gt;&lt;weight&gt;160&lt;/weight&gt;&lt;/connection number =6name=Uninordesc=Uninor&gt;&lt;connection number =7name=Airteldesc=Largets TRelecome &gt;&lt;ip_address&gt;192.168.1.23&lt;/ip_address&gt;&lt;subnet&gt;192.168.1.23&lt;/subnet&gt;&lt;gateway&gt;192.168.1.23&lt;/gateway&gt;&lt;probe_server1&gt;192.168.99.1&lt;/probe_server1&gt;&lt;probe_server2 /&gt;&lt;interface&gt;eth0&lt;/interface&gt;&lt;weight&gt;160&lt;/weight&gt;&lt;/connection number =7name=Airteldesc=Largets TRelecome &gt;&lt;/ispinfo&gt;
  </code></pre>
  
  <p>I just want to know that how can I write this XML in proper XML format ?</p>
  
  <p>Thanks in advance   </p>