在Python中动态生成XML

时间:2012-05-28 05:31:41

标签: python xml xml-serialization xml-parsing

嘿朋友我正在使用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?

提前致谢

2 个答案:

答案 0 :(得分:1)

更新以包括创建和打印XML树的模拟

基本问题

您的代码生成了无效的连接标记,如下所示:

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

当它们看起来像这样时(我省略了它们之间的子元素。你的代码正确地生成了这些元素):

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

如果你有有效的XML,这段代码会整齐地打印出来:

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)

生成有效XML

小模拟如下:

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)

这将产生:

<ispinfo>
  <connection desc="Largets TRelecome" number="5" name="Airtelll">
    <ip_address>192.168.1.23</ip_address>
  </connection>
</ispinfo>

答案 1 :(得分:0)

单行正确,从某种意义上说,XML解析器会理解它。

要对sys.stdout进行漂亮打印,请使用dump的{​​{1}}方法。

要对流进行漂亮打印,请使用Element的{​​{1}}方法。