龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > VB开发 >

vbscript 读取xml格式的配置文件

时间:2014-06-29 03:01来源:网络整理 作者:网络 点击:
分享到:
最近一项目中,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>
<!--メッセージ定義
-->
<MessageDefine>
<Info.001 value="処理開始。"/>
<Info.002 value="処理終了。"/>
<Info.003 value="処理異常終了。"/>
<Info.004 value="処理中止。"/>
</MessageDefine>
<!-- その他配置
-->
<OtherSection>
<host value="192.168.0.241"/>
<user value="root"/>
</OtherSection>
<WindowsLogToolConfig>
<host value="192.168.0.188"/>
<port value="3306"/>
</WindowsLogToolConfig>
</Configuration>
收藏文章
表情删除后不可恢复,是否删除
取消
确定
图片正在上传,请稍后...
评论内容为空!
还没有评论,快来抢沙发吧!
按钮 内容不能为空!
立刻说两句吧! 查看0条评论
精彩图集

赞助商链接