加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xml 转换conf 基于python

发布时间:2020-12-16 05:03:49 所属栏目:百科 来源:网络整理
导读:话部多说,xml格式如下: ?xml version="1.0" encoding="UTF-8"?confnova192.168.0.1/novaglance192.168.0.2/glancekeystone192.168.0.3/keystone/conf conf文件如下: [service_ip_list]keystone = X.X.X.Xnova = Xglance = X 需要提取xml中的属性和值替换

话部多说,xml格式如下:

<?xml version="1.0" encoding="UTF-8"?>
<conf>
<nova>192.168.0.1</nova>
<glance>192.168.0.2</glance>
<keystone>192.168.0.3</keystone>
</conf>

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()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读