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

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

XMLパーサーがそれを理解するという意味で、1行が適切です。

にきれいに印刷するには、の方法をsys.stdout使用します。dumpElement

ストリームにきれいに印刷するには、のwriteメソッドを使用しElementTreeます。

于 2012-05-28T05:41:46.217 に答える