python中创建xml文件用libxml2和xm.dom哪个简单些

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

python中创建xml文件用libxml2和xm.dom哪个简单些

jinyan

我是pyhton新手,目前要实现一个自动创建虚拟机的配置文件(xml格式的)昨天看了下xm.dom的一个例子,创建一个xml文件好像很复杂,一个同学说可以考虑下libxml2库。

刚看了下有几千行代码,头都大了。虚拟机的xml配置文件如下:

<domain type='xen' id='3'>

  <name>fv0</name>

  <uuid>4dea22b31d52d8f32516782e98ab3fa0</uuid>

  <os>

    <type>hvm</type>

    <loader>/usr/lib/xen/boot/hvmloader</loader>

    <boot dev='hd'/>

  </os>

  <memory>524288</memory>

  <vcpu>1</vcpu>

  <on_poweroff>destroy</on_poweroff>

  <on_reboot>restart</on_reboot>

  <on_crash>restart</on_crash>

  <features>

     <pae/>

     <acpi/>

     <apic/>

  </features>

  <clock sync="localtime"/>

  <devices>

    <emulator>/usr/lib/xen/bin/qemu-dm</emulator>

    <interface type='bridge'>

      <source bridge='xenbr0'/>

      <mac address='00:16:3e:5d:c7:9e'/>

      <script path='vif-bridge'/>

    </interface>

    <disk type='file'>

      <source file='/root/fv0'/>

      <target dev='hda'/>

    </disk>

    <disk type='file' device='cdrom'>

      <source file='/root/fc5-x86_64-boot.iso'/>

      <target dev='hdc'/>

      <readonly/>

    </disk>

    <disk type='file' device='floppy'>

      <source file='/root/fd.img'/>

      <target dev='fda'/>

    </disk>

    <graphics type='vnc' port='5904'/>

  </devices>

</domain>

要实现一个这样的配置文件,里面的内容要能动态修改,应该是传参数进去再生成。

请教下高手,我用这两个库中的任意一个该怎样实现,或者有甚么其它的方法。我只有一周时间了,谢谢啊!


_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: python中创建xml文件用libxml2和xm.dom哪个简单些

limodou
建议参考elementTree模块。

http://effbot.org/zone/xml-writer.htm

2008/1/22 jinyan <[hidden email]>:

>
>
>
>
> 我是pyhton新手,目前要实现一个自动创建虚拟机的配置文件(xml格式的)昨天看了下xm.dom的一个例子,创建一个xml文件好像很复杂,一个同学说可以考虑下libxml2库。
>
> 刚看了下有几千行代码,头都大了。虚拟机的xml配置文件如下:
>
> <domain type='xen' id='3'>
>
>   <name>fv0</name>
>
>   <uuid>4dea22b31d52d8f32516782e98ab3fa0</uuid>
>
>   <os>
>
>     <type>hvm</type>
>
>     <loader>/usr/lib/xen/boot/hvmloader</loader>
>
>     <boot dev='hd'/>
>
>   </os>
>
>   <memory>524288</memory>
>
>   <vcpu>1</vcpu>
>
>   <on_poweroff>destroy</on_poweroff>
>
>   <on_reboot>restart</on_reboot>
>
>   <on_crash>restart</on_crash>
>
>   <features>
>
>      <pae/>
>
>      <acpi/>
>
>      <apic/>
>
>   </features>
>
>   <clock sync="localtime"/>
>
>   <devices>
>
>     <emulator>/usr/lib/xen/bin/qemu-dm</emulator>
>
>     <interface type='bridge'>
>
>       <source bridge='xenbr0'/>
>
>       <mac address='00:16:3e:5d:c7:9e'/>
>
>       <script path='vif-bridge'/>
>
>     </interface>
>
>     <disk type='file'>
>
>       <source file='/root/fv0'/>
>
>       <target dev='hda'/>
>
>     </disk>
>
>     <disk type='file' device='cdrom'>
>
>       <source file='/root/fc5-x86_64-boot.iso'/>
>
>       <target dev='hdc'/>
>
>       <readonly/>
>
>     </disk>
>
>     <disk type='file' device='floppy'>
>
>       <source file='/root/fd.img'/>
>
>       <target dev='fda'/>
>
>     </disk>
>
>     <graphics type='vnc' port='5904'/>
>
>   </devices>
>
> </domain>
>
> 要实现一个这样的配置文件,里面的内容要能动态修改,应该是传参数进去再生成。
>
> 请教下高手,我用这两个库中的任意一个该怎样实现,或者有甚么其它的方法。我只有一周时间了,谢谢啊!
> _______________________________________________
> python-chinese
> Post: send [hidden email]
> Subscribe: send subscribe to [hidden email]
> Unsubscribe: send unsubscribe to  [hidden email]
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>



--
I like python!
UliPad <<The Python Editor>>: http://code.google.com/p/ulipad/
meide <<wxPython UI module>>: http://code.google.com/p/meide/
My Blog: http://www.donews.net/limodou
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese