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

VB scrpit 修改配置文件(读写XML)

发布时间:2020-12-16 22:59:04 所属栏目:大数据 来源:网络整理
导读:‘VB scrpit 修改配置文件(XML): Dim WshShellSet WshShell = CreateObject("Wscript.Shell")Set fso = CreateObject("Scripting.FileSystemObject")' Create log file for the script Set logStream = fso.createtextfile("updateConfig.log",True)logSt

‘VB scrpit 修改配置文件(XML):

Dim WshShell
Set WshShell = CreateObject("Wscript.Shell")

Set fso = CreateObject("Scripting.FileSystemObject")

' Create log file for the script  
Set logStream = fso.createtextfile("updateConfig.log",True)
logStream.writeline "Start update Config"  

' 假定要修改的为一个数据库配置文件,要更新DataSource 和DataSourceMirror 两个元素
dataSource = "localhost"
dataSourceMirror = "localhost" 

'假定要从另一个test.xml中读取DataSource 和DataSourceMirror 的值,然后更新到 configFile.config
testPath= "test.xml"
Set xmlDoc = CreateObject("Msxml2.DOMDocument") 
If (fso.FileExists(testPath)) Then
    msg = nBoxConfigFilePath & " exists"
    logStream.writeline  msg
     
    ' Load the test config file
     xmlDoc.load(testPath)
     
     ' 假定test.xml的结构相对复杂,用xpath搜索出对应的节点:    
     Set dataSourceNode = xmlDoc.selectSingleNode("//test[@name='DataSource']").parentNode
     Set dataSourceMirrorNode = xmlDoc.selectSingleNode("//test[@name='DataSourceMirror ']").parentNode
     
     ' 取得节点的属性值
     dataSource = dataSourceNode.getAttribute("name")
     dataSourceMirror = dataSourceMirrorNode.getAttribute("name")
  Else
    msg = "Warning" & testPath & " doesn't exists "   

    ' 如果testPath 路径不存在那么将警告信息写入log文件 
    logStream.writeline  msg   
  End If 
  
Set WshEnv = WshShell.Environment("PROCESS")

' 请注意将config dir 和configFile 替换成对应的内容 
filePath = “configdir/configFile.config" 

If (fso.FileExists(filePath)) Then
     msg = filePath & " exists"    

      ' 如果filePath 路径不存在那么将警告信息写入log文件 
     logStream.writeline  msg
     
     'Loading config file 
     xmlDoc.load(filePath) 
     logStream.writeline "Loading config file " & filePath
     
     'Get the datasource and mirror node
     Set rtDataSourceNode = xmlDoc.selectSingleNode("//add[@key='DataSource']") 
     Set rtDataSourceMirrorNode = xmlDoc.selectSingleNode("//add[@key='DataSourceMirror']")
     
     'Set the value attribute for the datasource and mirror node
     rtDataSourceNode.setAttribute "value",dataSource 
     rtDataSourceMirrorNode.setAttribute "value",dataSourceMirror     
     
     'Save the config file
     XMLDoc.save(filePath)
     logStream.writeline "Save the config file"
     logStream.writeline "Finish the configuration"
  Else
    msg = "Error:" & filePath & " doesn't exists"
  End If 
 logStream.Close

‘ 释放WshShell 
 Set WshShell = Nothing

configFile.config示例

<?xml version="1.0" encoding="utf-8"?> <configuration> <appSettings> <add key="DataSource" value="localhost" /> <add key="DataSourceMirror" value="localhost" /> <add key="InitialCatalog" value="TestDB" /> </appSettings> </configuration>

(编辑:李大同)

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

    推荐文章
      热点阅读