vbscript 读取xml格式的配置文件

爱蒂网

vbscript 读取xml格式的配置文件

2020-03-11 19:30:49 分类 / 代码专栏 来源 / 互联网

最近一项目中,vbs脚本需要读取配置文件,本来考虑用ini来做配置文件,但是vbs里没有现成读写ini文件的支持,于是考虑用xml来做配置文件,使用xmldom来读取。写成个class使用起来应该方便

复制代码 代码如下:
Class clsGetProfile
' ル`トドキュメント
Private rootDoc
' xmlファイル名とセクション名をセットする
' 引数: 「1」ファイル名 NOT NULL
' 氦胜
Public Sub setProfile(strFileName)
Set data_xml = CreateObject("Microsoft.XMLDOM")
data_xml.async = False
data_xml.load(strFileName)
Set rootDoc = data_xml.documentElement
End Sub
' キ`のする蛉〉盲工
' 引数: 「1」キ`名  NOT NULL
' 「2」セクション名 NOT NULL
' 亥`のする
Public Function getItem(strSectionName, itemName)
Set sectionNode = rootDoc.selectSingleNode(strSectionName)
getItem = sectionNode.selectSingleNode(itemName).attributes(0).nodeValue
End Function
End Class
' 使用サンプル
' クラスインスタンスを生成する
'Dim config : Set config = New clsGetProfile
' 配置ファイル名とセクション名をセットする
'Call config.setProfile("Config.xml")
' き`のする颔播氓趣工
'WScript.Echo config.getItem("MessageDefine", "INFO.001")
'WScript.Echo config.getItem("MessageDefine", "INFO.003")
'WScript.Echo config.getItem("MessageDefine", "ERROR.009")
'WScript.Echo config.getItem("MessageDefine", "ERROR.012")
Class clsGetProfile
' ル`トドキュメント
Private rootDoc
' xmlファイル名とセクション名をセットする
' 引数: 「1」ファイル名 NOT NULL
' 氦胜
Public Sub setProfile(strFileName)
Set data_xml = CreateObject("Microsoft.XMLDOM")
data_xml.async = False
data_xml.load(strFileName)
Set rootDoc = data_xml.documentElement
End Sub
' キ`のする蛉〉盲工
' 引数: 「1」キ`名  NOT NULL
' 「2」セクション名 NOT NULL
' 亥`のする
Public Function getItem(strSectionName, itemName)
Set sectionNode = rootDoc.selectSingleNode(strSectionName)
getItem = sectionNode.selectSingleNode(itemName).attributes(0).nodeValue
End Function
End Class
' 使用サンプル
' クラスインスタンスを生成する
'Dim config : Set config = New clsGetProfile
' 配置ファイル名とセクション名をセットする
'Call config.setProfile("Config.xml")
' き`のする颔播氓趣工
'WScript.Echo config.getItem("MessageDefine", "INFO.001")
'WScript.Echo config.getItem("MessageDefine", "INFO.003")
'WScript.Echo config.getItem("MessageDefine", "ERROR.009")
'WScript.Echo config.getItem("MessageDefine", "ERROR.012")

配置文件格式:
复制代码 代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<!--メッセ`ジ定x
-->
<MessageDefine>
<Info.001 value="I理_始。"/>
<Info.002 value="I理K了。"/>
<Info.003 value="I理常K了。"/>
<Info.004 value="I理中止。"/>
</MessageDefine>
<!-- その他配置
-->
<OtherSection>
<host value="192.168.0.241"/>
<user value="root"/>
</OtherSection>
<WindowsLogToolConfig>
<host value="192.168.0.188"/>
<port value="3306"/>
</WindowsLogToolConfig>
</Configuration>

猜你喜欢