Solved

How to read an XML return string from within VBScript?

Posted on 2001-07-17
5
3,450 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
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now