Solved

How to read an XML return string from within VBScript?

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

688 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