1

嘿朋友们,我正在使用 Python 库生成 XML 数据,如下所示

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

但我得到的输出如下

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

我只想知道如何以正确的 XML 格式编写此 XML?

提前致谢

4

2 回答 2

3

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>
于 2012-05-28T06:38:41.223 回答
0

单行是正确的,因为 XML 解析器会理解它。

为了漂亮打印到sys.stdout,请使用 的dump方法Element

要对流进行漂亮打印,请write使用ElementTree.

于 2012-05-28T05:41:46.217 回答