Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8029841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:33:30+00:00 2026-06-05T00:33:30+00:00

Hey friends I am generating XML data using Python libraries as follow def multiwan_info_save(request):

  • 0

Hey friends I am generating XML data using Python libraries as follow

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 +++++++++++++++++++++++++"

But I am getting output like follow

<ispinfo><connection number =5name=Airtelllldesc=Largets TRelecome ><ip_address>192.168.1.23</ip_address><subnet>192.168.1.23</subnet><gateway>192.168.1.23</gateway><probe_server1>192.168.99.1</probe_server1><probe_server2 /><interface>eth0</interface><weight>160</weight></connection number =5name=Airtelllldesc=Largets TRelecome ><connection number =6name=Uninordesc=Uninor><ip_address>192.166.55.23</ip_address><subnet>192.166.55.23</subnet><gateway>192.168.1.23</gateway><probe_server1>192.168.99.1</probe_server1><probe_server2 /><interface>eth0</interface><weight>160</weight></connection number =6name=Uninordesc=Uninor><connection number =7name=Airteldesc=Largets TRelecome ><ip_address>192.168.1.23</ip_address><subnet>192.168.1.23</subnet><gateway>192.168.1.23</gateway><probe_server1>192.168.99.1</probe_server1><probe_server2 /><interface>eth0</interface><weight>160</weight></connection number =7name=Airteldesc=Largets TRelecome ></ispinfo>

I just want to know that how can I write this XML in proper XML format ?

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T00:33:34+00:00Added an answer on June 5, 2026 at 12:33 am

    UPDATED to include simulation of both creating and printing of the XML tree

    The Basic Issue

    Your code is generating invalid connection tags like this:

    <connection number =5name=Airtelllldesc=Largets TRelecome ></connection number =5name=Airteldesc=Largets TRelecome >
    

    when they should look like this (I am omitting the sub-elements in between. Your code is generating these correctly):

    <connection number="5" name="Airtellll" desc="Largets TRelecome" ></connection>
    

    If you had valid XML, this code would print it neatly:

    from lxml import etree
    xml = '''<ispinfo><connection number="5" name="Airtellll" desc="Largets TRelecome" ><ip_address>192.168.1.23</ip_address><subnet>192.168.1.23</subnet><gateway>192.168.1.23</gateway><probe_server1>192.168.99.1</probe_server1><probe_server2 /><interface>eth0</interface><weight>160</weight></connection></ispinfo>'''
    xml = etree.XML(xml)
    print etree.tostring(xml, pretty_print = True)
    

    Generating Valid XML

    A small simulation follows:

    from lxml import etree
    
    # Some dummy text
    conn_id = 5
    conn_name = "Airtelll"
    conn_desc = "Largets TRelecome"
    ip = "192.168.1.23"
    
    # Building the XML tree
    # Note how attributes and text are added, using the Element methods
    # and not by concatenating strings as in your question
    root = etree.Element("ispinfo")
    child = etree.SubElement(root, 'connection',
                     number = str(conn_id),
                     name = conn_name,
                     desc = conn_desc)
    subchild_ip = etree.SubElement(child, 'ip_address')
    subchild_ip.text = ip
    
    # and pretty-printing it
    print etree.tostring(root, pretty_print=True)
    

    This will produce:

    <ispinfo>
      <connection desc="Largets TRelecome" number="5" name="Airtelll">
        <ip_address>192.168.1.23</ip_address>
      </connection>
    </ispinfo>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey Friends I want to make a form builder using Yii. For that I
Hey friends i have use 2 text view now when i fetch data from
Hey Friends Here is the table details and data id data h1 h2 h3
Hey. I only recently started using Python, and my friend suggested using Twisted as
Hey, friends, I am using squeak for developing and I found primitives is useful,
Hey, my friends and I are trying to beat each other's runtimes for generating
Hey Friends i got all contacts detail of my iphone by using this function
Hey Friends, I am using the following API for getting details of IMDB,http://www.deanclatworthy.com/imdb/?q=Star+Trek while
Hey Friends I've got another xsl/xpath problem. Sure a beginners problem, but I'm thinking
Hey, say you have an array of friends that you've retrieved, how can you

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.