Loop through REST XML Response data

jfergy used Ask the Experts™

I want to load the response string from a REST call to XML, then iterate through the responses. I can output the response just fine as a string, how do I now iterate or loop through the response to control how its output?
' Create the xml object 
 Set GetConnection = CreateObject("Microsoft.XMLHTTP")

 ' Conect to specified URL (API)
 GetConnection.Open "get", URL, False 
 ' ResponsePage is the response we will get when visiting DocStocLoginURL
 ResponsePage = GetConnection.responseText 
' We will write  
 'xmlResponse = ResponsePage.toXML()
  Set GetConnection = Nothing
 On Error Goto 0
 if ResponsePage = "" then
 end if

Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

You can load the response xml in a dom object and then retrieve data of any specific node or loop it as desired

something like this

Set objXML = Server.CreateObject("Microsoft.XMLDOM")
objXML.async = False
objXML.LoadXML (responsePage)
If objXML.parseError.errorCode <> 0 Then
   Response.Write "Error in loading xml"
End If

Set objLst = objXML.getElementsByTagName("*")
For i = 0 to (objLst.length – 1)
   Response.Write objLst.item(i).item & "<BR>"

Open in new window


Im not getting an error, but I am also not getting any items. I've checked the case of the TagName and still nothing?


To give you an Idea, here is the XML returned
<?xml version="1.0" encoding="utf-8"?>
<DocumentSearchResults xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <Title />
      <Filename>Appraisal Form.doc</Filename>
      <DisplayName>Appraisal Form</DisplayName>
      <Description>Appraisal Form We have carefully and thoroughly examined the article</Description>
      <ScName>Sales &amp; Marketing </ScName>
      <Sc2Name />

Open in new window

JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

try this

Set objLst = objXML.getElementsByTagName("*")
For i = 0 to (objLst.length - 1)
   Response.Write objLst.item(i).text & "<BR>"

also ensure that your xml response is properly formated.. The posted xml is not in correct format ie closing tag for <Document> is missing.


Thank you! That worked great.


I know this question is related but could you help with this?

If I wanted to return several values at once like as in a recordset return from the xml how could I do that? I know I can do the following:

if objLst.item(i).nodename = "DisplayName" then
    DisplayName = objLst.item(i).text
    end if

But I need to return several values from each record at once, not just one at a time. so I need for example 5 of the nodes per record and their values so i can assign them variables to output to my page.
can you explain ur need in respect to the xml input you provided..


I am needing to pull 5 nodes and their text in the node back for each documentlistitem in the xml results.
So for each <DocumentListItem> I need to grab the following:

why dont you create new question so that i post a solution of it there ?


here it is: http://www.experts-exchange.com/Web_Development/Web_Services/REST/Q_25099641.html

I didnt have enough points left to do more than 35. Sorry
never mind about points.. i responded to your new question

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial