一、删除xml的文件头
由于我需要的xml不需要python自动生成如下这一行
<?xml version="1.0" ?>
所以,需要修改python库。
vim /usr/lib64/python2.7/xml/dom/minidom.py
如下,删除1747行到1750行。
二、python代码
#coding:utf-8 from xml.dom import minidom #写入xml文档的方法 def create_xml_test(filename): xml=minidom.Document() include=xml.createElement('include') xml.appendChild(include) gateway=xml.createElement('gateway') gateway.setAttribute('name',filename) include.appendChild(gateway) param=xml.createElement('param') param.setAttribute('name','realm') param.setAttribute('value','zxr2016.com') gateway.appendChild(param) param=xml.createElement('param') param.setAttribute('name','proxy') param.setAttribute('value','192.168.1.100:5060') gateway.appendChild(param) param=xml.createElement('param') param.setAttribute('name','register') param.setAttribute('value','true') gateway.appendChild(param) param=xml.createElement('param') param.setAttribute('name','caller-id-in-from') param.setAttribute('value','true') gateway.appendChild(param) param=xml.createElement('param') param.setAttribute('name','username') param.setAttribute('value',filename) gateway.appendChild(param) param=xml.createElement('param') param.setAttribute('name','password') param.setAttribute('value',filename) gateway.appendChild(param) f=open(filename + ".xml",'w') f.write(xml.toprettyxml()) f.close() def main(): for i in range(1003, 1305): create_xml_test(str(i)) if __name__=='__main__': main()
三、生成的xml例子:
<include> <gateway name="1160"> <param name="realm" value="zxr2016.com"/> <param name="proxy" value="192.168.1.100:5060"/> <param name="register" value="true"/> <param name="caller-id-in-from" value="true"/> <param name="username" value="1160"/> <param name="password" value="1160"/> </gateway> </include>