xml 转换conf 基于python

话部多说,xml格式如下:



192.168.0.1
192.168.0.2
192.168.0.3

conf文件如下:

[service_ip_list]
keystone = X.X.X.X
nova = X
glance = X

需要提取xml中的属性和值替换掉conf中的对应的配置,转换程序是用python写的

#-*-coding:utf-8-*-
from xml.dom import minidom
doc = minidom.parse("conf.xml")
conf_elements=doc.getElementsByTagName("conf")[0]

nova=conf_elements.getElementsByTagName("nova")[0]
#print nova.nodeName, nova.childNodes[0].nodeValue

glance=conf_elements.getElementsByTagName("glance")[0]
#print glance.nodeName, glance.childNodes[0].nodeValue

keystone=conf_elements.getElementsByTagName("keystone")[0]
#keystone_ip=keystone.childNodes[0].nodeValue
#print keystone.nodeName, keystone.childNodes[0].nodeValue

import ConfigParser
import string,os,sys

cf=ConfigParser.ConfigParser()
cf.read("ip-list.conf")
cf.set("service_ip_list","keystone",keystone.childNodes[0].nodeValue)
cf.set("service_ip_list","nova",nova.childNodes[0].nodeValue)
cf.set("service_ip_list","glance",glance.childNodes[0].nodeValue)
fh=open("ip-list.conf",'w')
cf.write(fh)
fh.close()


你可能感兴趣的:(Linux)