资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

VBScript读取XML文件-创新互联

虽然现今数据大部分都是使用数据库存储,但是停电断网等不可抗拒因素经常中断着automation的进行,这时若这些非业务的配置数据用xml来存储就显得方便很多了,下面是用VBScript脚本读取XML文件的实现方法,记录下以作备忘。

从策划到设计制作,每一步都追求做到细腻,制作可持续发展的企业网站。为客户提供成都网站制作、成都做网站、网站策划、网页设计、主机域名雅安服务器托管、网络营销、VI设计、 网站改版、漏洞修补等服务。为客户提供更好的一站式互联网解决方案,以客户的口碑塑造优易品牌,携手广大客户,共同发展进步。

1. XML文件的节点信息:



  
    1
    TNSLogon
 ELOG
 ASD
    asd
    1
  
  
    3
    TNSLogon
 NAV12C2
 navdev
    navdev
    0
  
  
    5
    DirectLogon
 RTTD
 devman
    devman
    0
  

2. 判断当前系统的MSXML.dll组件的版本(由于系统中一般会有多个版本,这里就只取大的版本)

Function iGetMSxmlVersion()
    Dim SystemPath, fso, f, fa, fname, iVersion, iMaxVersion
    SystemPath =  "C:\Windows\System32" 'system  path
    Set fso = CreateObject("Scripting.FileSystemObject")
    If fso.FolderExists(SystemPath) Then
        Set f = fso.GetFolder(SystemPath)
        Set fa = f.Files
        iMaxVersion = 0
        ' calc the amount of files 
        For Each fname in fa
            If Instr(fname,"msxml")>0 Then
                iVersion = Mid(fname.name,6,1)
                If IsNumeric(iVersion) Then
                    If iMaxVersion < iVersion Then
                        iMaxVersion = iVersion
                    End If
                End If
            End If      
        Next
        If iMaxVersion >0 Then
            iGetMSxmlVersion = iMaxVersion
            Log.Message("The max MSxml version is: " + iMaxVersion) 
        Else
            iGetMSxmlVersion = 0
            Log.Error("The current system not exist MSxml module.") 
        End If      
    End If
End Function

3. 读取xml文件函数:

Function sGetDataFromXML(sXmlPath, sRoot, sNode, iDataID, sColumn) 
    Dim xmlVersion
    xmlVersion = iGetMSxmlVersion
    If sXmlPath <> "" and  sRoot <> "" and sNode <> "" and iDataID <>"" and sColumn <> "" Then
        If xmlVersion <> 0 Then
            ' Create COM object
            Set Doc = Sys.OleObject("Msxml2.DOMDocument."+ xmlVersion +".0")
            ' you can also use the following method to create XMLDOM object
            'Set Doc = CreateObject("Microsoft.XMLDOM")
            Doc.async = False
            ' Load data from a file
            ' We use the file created earlier
            Call Doc.load(sXmlPath)
            ' Report an error, if, for instance, the markup or file structure is invalid 
            If Doc.parseError.errorCode <> 0 Then
                s = "Reason:" + Chr(9) + Doc.parseError.reason + _
                    "Line:" + Chr(9) + CStr(Doc.parseError.line) + Chr(13) + Chr(10) + _
                    "Pos:" + Chr(9) + CStr(Doc.parseError.linePos) + Chr(13) + Chr(10) + _
                 "Source:" + Chr(9) + Doc.parseError.srcText
                ' Post an error to the log and exit
                Call Log.Error("Cannot parse the document.", s) 
                Exit Function
            End If
            ' Use an XPath expression to obtain a list of "control" nodes 
            Set Nodes = Doc.selectNodes("/" + sRoot + "/" + sNode + "[DataID=" + CStr(iDataID) + "]/" + sColumn)
            If Not(Nodes.item(0) is Nothing) Then
                Call Log.Message("Data from XML: " + Nodes.item(0).text) 
                sGetTestDataFromXML = Nodes.item(0).text
            Else
                Log.Error("Read XML nodes occur errors.")
                Exit Function
            End If
            Set Nodes = Nothing
            Set Doc = Nothing
        Else
            Exit Function
        End If
    Else
        Log.Error("Function sGetTestDataFromXML() occur incorrect parameters.")
        Exit Function
    End If
End Function

4. TestComplete中测试脚本及log输出:

Sub Test
    Dim strXmlPath, strTNSName, strUserame, strPassword
    strXmlPath=Project.Path + "Data\TC_TNSInfo.xml"
    log.Message(strXmlPath)
    If aqFile.Exists(strXmlPath) Then
        strTNSName = sGetTestDataFromXML(strXmlPath, "TC_TestLogon", "TNSLogon", 3, "I_TNSName")
        strUserame = sGetTestDataFromXML(strXmlPath, "TC_TestLogon", "TNSLogon", 3, "I_Username")
        strPassword = sGetTestDataFromXML(strXmlPath, "TC_TestLogon", "TNSLogon", 3, "I_Password")
        log.Message("TNSName is :"+strTNSName)
        log.Message("Username is :"+strUserame)
        log.Message("Password is :"+strPassword)
    End If
End Sub

VBScript读取XML文件

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


标题名称:VBScript读取XML文件-创新互联
网站网址:http://cdkjz.cn/article/ihpcj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220