VBScript reads the XML file

Source: Internet
Author: User

Although most of today's data are used in database storage, but power outages and other irresistible factors often interrupt the automation, when these non-business configuration data with XML to store it is much more convenient, the following is the use of VBScript script to read XML file implementation method, Record to make a memo.

1. Node information for the XML file:

<?xml version= "1.0"?><tc_testlogon>  <tnslogon>    < Dataid>1</dataid>    <i_type>tnslogon</i_type> <i_tnsname> Elog</i_tnsname> <i_username>asd</i_username>    <i_password> asd</i_password>    <i_savepassword>1</i_savepassword>  </ tnslogon>  <tnslogon>    <dataid>3</dataid>     <i_type>tnslogon</i_type> <i_tnsname>nav12c2</i_tnsname> <i_ username>navdev</i_username>    <i_password>navdev</i_password>     <I_SavePassword>0</I_SavePassword>  </TNSLogon>  < Tnslogon>    <dataid>5</dataid>    <i_type> Directlogon</i_type> <i_tnsname>rttd</i_tnsname> <i_username>devman</i_username>     <i_password>devman</i_password>    <i_savepassword>0</i_ Savepassword>  </tnslogon></tc_testlogon>

2. Determine the version of the MSXML.dll component of the current system (since there are generally multiple versions in the system, only the largest version is available here)

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") &GT;0&NBSP;THEN&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSp;          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 ifend function

3. Read the XML file function:

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:" &NBSP;+&NBSP;CHR (9)  +  doc.parseerror.reason + _                      "line:" &NBSP;+&NBSP;CHR (9)  + cstr ( Doc.parseError.line) &NBSP;+&NBSP;CHR () &NBSP;+&NBSP;CHR (Ten)  + _                      "Pos:" &NBSP;+&NBSP;CHR (9)  + cstr (Doc.parseError.linePos)  + chr (&NBSP;+&NBSP;CHR)  + _                   "Source:"  + &NBSP;CHR (9)  + Doc.parseError.srcText                  '  Post an error to the log and  exit          &Nbsp;     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 IfEnd  Function

4. Test script and log output in Testcomplete:

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&NBSP;IS&NBSP;:" +strtnsname)         log. Message ("USERNAME&NBSP;IS&NBSP;:" +struserame) &NBSp;       log. Message ("PASSWORD&NBSP;IS&NBSP;:" +strpassword)     end ifend sub

650) this.width=650; "title=" QQ picture 20150122115040.jpg "alt=" wkiol1tadh3iysp_aabsy3frakw996.jpg "src="/http S3.51cto.com/wyfs02/m01/58/e6/wkiol1tadh3iysp_aabsy3frakw996.jpg "/>

This article is from the "Qytag (upspringing)" blog, so be sure to keep this source http://qytag.blog.51cto.com/6125308/1606935

VBScript reads the XML file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.