Loop through REST XML Response data

jfergy
jfergy used Ask the Experts™
on
Hello-

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 
 GetConnection.Send  
 
 ' ResponsePage is the response we will get when visiting DocStocLoginURL
 ResponsePage = GetConnection.responseText 
 
' We will write  
 response.write(responsePage)
 'xmlResponse = ResponsePage.toXML()
  Set GetConnection = Nothing
 
 On Error Goto 0
 if ResponsePage = "" then
	Response.Write("Invalid")
 end if

Open in new window

Comment
Watch Question

Do more with

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

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"
   Response.End
End If

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

Open in new window

Author

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

Author

Commented:
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">
  <Source>Cache</Source>
  <ResultType>Complete</ResultType>
  <TotalResults>229</TotalResults>
  <StartIndex>0</StartIndex>
  <Documents>
    <DocumentListItem>
      <FolderID>0</FolderID>
      <SortBy>FileName</SortBy>
      <IsSelected>false</IsSelected>
      <DocumentURL>/docs/5220/Appraisal-Form</DocumentURL>
      <DocType>0</DocType>
      <IsPrivate>false</IsPrivate>
      <DocID>5220</DocID>
      <MemID>-10</MemID>
      <MemName>anonymous</MemName>
      <Title />
      <Filename>Appraisal Form.doc</Filename>
      <DisplayName>Appraisal Form</DisplayName>
      <Description>Appraisal Form We have carefully and thoroughly examined the article</Description>
      <Tags>Sales</Tags>
      <DateUploaded>2007-07-07T14:02:05</DateUploaded>
      <AverageRating>0</AverageRating>
      <RatingCount>0</RatingCount>
      <ViewCount>156</ViewCount>
      <DownloadCount>3</DownloadCount>
      <ReviewCount>0</ReviewCount>
      <DocLanguage>English</DocLanguage>
      <CategoryName>Business</CategoryName>
      <ScName>Sales &amp; Marketing </ScName>
      <Sc2Name />
      <CategoryID>2</CategoryID>
      <ScID>75</ScID>
      <Sc2ID>0</Sc2ID>
      <ProcessingStatus>NotProcessed</ProcessingStatus>
      <DateUpdated>0001-01-01T00:00:00</DateUpdated>
      <DailyViewCount>0</DailyViewCount>
      <DailyDownloadCount>0</DailyDownloadCount>
      <DailyCommentsCount>0</DailyCommentsCount>
      <DailyRatingAvg>0</DailyRatingAvg>
      <WeeklyViewCount>1</WeeklyViewCount>
      <WeeklyDownloadCount>1</WeeklyDownloadCount>
      <WeeklyCommentsCount>0</WeeklyCommentsCount>
      <WeeklyRatingAvg>0</WeeklyRatingAvg>
      <IsPaidDoc>false</IsPaidDoc>
      <IsProDoc>false</IsProDoc>
      <BasePrice>0</BasePrice>
      <NumberOfPages>1</NumberOfPages>
    </DocumentListItem>
</DocumentSearchResults>

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>"
Next

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

Author

Commented:
Thank you! That worked great.

Author

Commented:
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..

Author

Commented:
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:
<MemID/>
<DisplayName/>
<DocumentURL/>
<DocID/>
<FileName/>

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

Author

Commented:
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