简体   繁体   中英

Getting the recordset into an XML using VBScript

My SQL query returns a single column result which is actually an XML. When I click on the recordset, it opens up a results.xml .

This is the output XML I am grabbing into result_string by using

result_string = objRecordSet.Fields(colname).Value
<TEST_SUITE SUITE_ID="122675741" PART_NUMBER="MTSTE-26248-00" REVISION="A"     SUITE_TYPE="PRODUCTION" SUITE_NAME="HOT-LOW" SUITE_STOP_ON_FAIL="TRUE" TARGET_DURATION="0" SEQUENCE="1" UPDATED="2016-12-15T00:21:46.510">
  <SUITE_DESCRIPTION>"HOT-LOW"</SUITE_DESCRIPTION>
  <TESTS>
    <TEST TEST_ID="122663528" TEST_NAME="Set Suite Environment Variables" PART_NUMBER="MTTST-65899-00" REVISION="A" MODULE="TEST Set Environment Variables.vi" MODULE_LIBRARY="BIG-IP_TESTS" TEST_STOP_ON_FAIL="TRUE"   TEST_TYPE="BEGIN_SESSION">
      <PARAMETERS>
        <PARAMETER PARAMETER_ID="122663469" PARAMETER_NAME="TOS" PARAMETER_VALUE="build_2.0.1.1647.0_centos6_x86_64" />
        <PARAMETER PARAMETER_ID="82137" PARAMETER_NAME="chain" PARAMETER_VALUE="TRUE" />
      </PARAMETERS>
      <MEASUREMENTS>
        <MEASUREMENT MEASUREMENT_ID="70784" MEASUREMENT_NAME="TOS" UNITS="" UPPER_LIMIT="" LOWER_LIMIT="" COMPARISON="LOG" INPUT_SCALE_FACTOR="" OUTPUT_SCALE_FACTOR="" />
        <MEASUREMENT MEASUREMENT_ID="81537" MEASUREMENT_NAME="chain" UNITS="" UPPER_LIMIT="" LOWER_LIMIT="" COMPARISON="LOG" INPUT_SCALE_FACTOR="" OUTPUT_SCALE_FACTOR="" />
      </MEASUREMENTS>
    </TEST>
  </TESTS>
</TEST_SUITE>

from this I need one Test_ID. How can I store into XML this result_string and get the Test_ID. Or is there any easy way to get the Test_ID?

I did something like this, but not sure if this is correct, and also I am getting an error on line objCommand.Execute ,, adExecuteStream .

Dim strm  
Set strm = CreateObject("ADODB.Stream")

strm.Open

Set objCommand.ActiveConnection = objConnection
objCommand.CommandText = script_testID
objCommand.Properties("Output Stream")= strm
objCommand.Properties("Output Encoding") = "UTF-8"
objCommand.Properties("XML Root") = "Root"  'this can be anything you want

objCommand.CommandType = 4
objCommand.Execute ,, adExecuteStream
strm.Position = 0
text = strm.ReadText
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.Async = "false"

xmlDoc.LoadXML(strm.ReadText)
strm.Close : Set strm = Nothing
Set objCommand = Nothing

You're already parsing the XML data, so you can easily extract the value like this:

...
xmlDoc.LoadXML(strm.ReadText)

test_id = xmlDoc.SelectSingleNode("//TEST/@TEST_ID").Value
...

I would recommend using Msxml2.DOMDocument instead of the deprecated Microsoft.XMLDOM , though, and I also recommend checking for parse errors before working with the data:

...
Set xmlDoc = CreateObject("Msxml2.DOMDocument.6.0")
xmlDoc.Async = False
xmlDoc.LoadXML strm.ReadText

If xmlDoc.ParseError <> 0 Then
    WScript.Echo xmlDoc.ParseError.Reason
    WScript.Quit 1
End If

test_id = xmlDoc.SelectSingleNode("//TEST/@TEST_ID").Value
...

Note that the XPath expression ( //TEST/@TEST_ID ) is case-sensitive.

test_id = Split(Split(result_string, "TEST_ID=""", 2)(1), """", 2)(0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM