Solved

How to read an XML return string from within VBScript?

Posted on 2001-07-17
5
3,757 Views
Last Modified: 2012-06-27
From within a .vbs program, I call an API which returns to me as a return code an XML string.

There are only certain 'element' values that I need to retrieve from the string, and I know there must be a better way of doing it than manually searching through a string of characters for what I want.

Here's a simplified version of my XML string:

<RETURN_CODE>
      <SUMMARY>
            <NUMBER_PROCESSED> 4 </NUMBER_PROCESSED>
            <NUMBER_REQUESTED> 4 </NUMBER_REQUESTED>
            </ERROR_CODE>
      </SUMMARY>
</RETURN_CODE>

So I currently have this XML return code assigned to a variable, as follows:


Dim object
Dim returnCode

returnCode = object.methodThatReturnsXMLResult()


Now, can I somehow put the contents of 'returnCode' (the XML string above) into an XML DOM and then query it for the values of, lets say, <NUMBER_PROCESSED> and/or <NUMBER_REQUESTED> ?

Code/syntax to do this is much appreciated!

Thanks!
0
Comment
Question by:BrianAbrahamson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 6291599
>>From within a .vbs program, I call an API which returns to me as a return code an XML string

what API function are you using?  is it truly an exported function of a dynamic link library?
0
 

Author Comment

by:BrianAbrahamson
ID: 6291633
I create an object from the instantiation of a DLL - the DLL is for a 3rd party product we have purchased.  I then call a method of that object, passing it the appropriate parameters.  That method has a return value, and the return value is an XML string.  

i.e.

returnCode = object.theMethodThatReturnsXML(param 1, param2)

If we were to do a 'msgbox' on the variable 'returnCode' -
i.e. msgbox(returnCode)

the result would literally be:
 "<RETURN_CODE><SUMMARY><NUMBER_PROCESSED>4</NUMBER_PROCESSED>....."   - you get the picture...  

If I need the value between the <NUMBER_PROCESSED> tags, I would like to be able to access the nodes/elements of this XML string via the XMLDOM or something instead of parsing through it manually to match on substrings or something.  

Please note that my example is simplified, so parsing through looking for a substring and then grabbing the value '4' or something is not feasible.

Thanks.
0
 

Author Comment

by:BrianAbrahamson
ID: 6291677
I create an object from the instantiation of a DLL - the DLL is for a 3rd party product we have purchased.  I then call a method of that object, passing it the appropriate parameters.  That method has a return value, and the return value is an XML string.  

i.e.

returnCode = object.theMethodThatReturnsXML(param 1, param2)

If we were to do a 'msgbox' on the variable 'returnCode' -
i.e. msgbox(returnCode)

the result would literally be:
 "<RETURN_CODE><SUMMARY><NUMBER_PROCESSED>4</NUMBER_PROCESSED>....."   - you get the picture...  

If I need the value between the <NUMBER_PROCESSED> tags, I would like to be able to access the nodes/elements of this XML string via the XMLDOM or something instead of parsing through it manually to match on substrings or something.  

Please note that my example is simplified, so parsing through looking for a substring and then grabbing the value '4' or something is not feasible.

Thanks.
0
 
LVL 5

Accepted Solution

by:
mattyk earned 50 total points
ID: 6291681
This hsould give you the syntax you're looking for:

Dim xmlDoc
Set xmlDoc = WScript.CreateObject("Microsoft.XMLDOM")
xmlDoc.loadxml(object.methodThatReturnsXMLResult())


Dim objNumReqs
Dim objNumReq
Set objNumReqs = xmlDoc.getElementsByTagName("NUMBER_REQUESTED")

for each objNumReq in objNumReqs
     msgbox objNumReq.text
Next

-matty
0
 

Expert Comment

by:Yogi012398
ID: 7239152
Instead of using the getElementsByTagName, try selectSingleNode. By adding two forward slashes in front of the node, it will search the whole xml doc for the node and retrieve the value.

Set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = False
xml.setProperty "ServerXMLHTTP", true
xml.load sPath 'Path of xml doc
sValue = xml.selectSingleNode("//NUMBER_PROCESSED").text

-Yogi
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question