Intro
Just a quick code sample that you can use in reference to working with various XML web services in Classic ASP. From here hopefully you can get a general idea of how it works – and expand it into something that will better fit your needs
The Goal
Let’s say that we want to request all of the groups that my gamertag is a member of at 360voice. Below is a full featured example (with some code comments) to accomplish this goal. We want to request information from the 360voice API, specifically the Groups API.
The Goods
First of all as with many other things in programming there are a few ways to do things. You could use different objects to pull the XML or you could use an XSL style sheet, or a number of other ways to work with the data, so play around, and look around – there are definitely other ways than my examples.
First of all we want to declare our variables, set up our objects and get the XML from the API
Dim objXML, url, source, group_nodes
'this is our HTTP Object to request the data
set objXML = Server.CreateObject("Microsoft.XMLHTTP")
'the page we want to load
url = "http://360voice.gamerdna.com/api/gamertag-groups.asp?tag=squidpunch"
'open the request
objXML.Open "GET", url, false
'actually send the request
objXML.Send
'we want an xml object to work with
set source = Server.CreateObject("Microsoft.XMLDOM")
'load the data from the returned value of the HTTP GET request
source.async = false
source.load(objXML.ResponseXML)
Now that we have the xml stored in the source object we just need to navigate the xml to get the data we want. If you load the example API from the Documentation, you will the proper structure of the xml document, here is a quick snippet of the API in question
1.0 squidpunch 635 Squidpunch Fan Club 11/17/2008 http://www.360voice.com/group/635 http://www.squidpunch.com I am a fan of myself. 1
Our goal is to get all the groups the gamertag is a member of and output some results – using the XML above we need to look in each group node – and pull out the items we want to display, for this example we are going to use the group name, group description and the group blog url. We do this by getting the proper nodes from the xml – and then selecting the text within – and we will output it to the screen, nothing fancy or flashy but it works.
'get all the group nodes
set group_nodes = source.getElementsByTagName("group")
'for sanity sake, check that there is at least 1 node
if group_nodes.length > 0 then
for x = 0 to group_nodes.length -1
dim groupname, groupdescription, groupurl
groupname = group_nodes(x).getElementsByTagName("name")(0).text
groupdescription = group_nodes(x).getElementsByTagName("description")(0).text
groupurl = group_nodes(x).getElementsByTagName("url")(0).text
response.write("" & groupname & "")
response.write(groupdescription & "")
response.write("Visit This Group
")
next
else
response.write "crash, boom, fail......"
end if
finally we need to clean up those nasty objects!
'cleanup set objXML = nothing set source = nothing set group_nodes = nothing
There you have it a start to finish on pulling XML data via the 360voice public APIs – hopefully this was a help, and good luck!
full code view:
<%
Dim objXML, url, source, group_nodes
'this is our HTTP Object to request the data
set objXML = Server.CreateObject("Microsoft.XMLHTTP")
'the page we want to load
url = "http://www.360voice.com/api/gamertag-groups.asp?tag=squidpunch"
'open the request
objXML.Open "GET", url, false
'actually send the request
objXML.Send
'we want an xml object to work with
set source = Server.CreateObject("Microsoft.XMLDOM")
'load the data from the returned value of the HTTP GET request
source.async = false
source.load(objXML.ResponseXML)
'get all the group nodes
set group_nodes = source.getElementsByTagName("group")
'for sanity sake, check that there is at least 1 node
if group_nodes.length > 0 then
for x = 0 to group_nodes.length -1
dim groupname, groupdescription, groupurl
groupname = group_nodes(x).getElementsByTagName("name")(0).text
groupdescription = group_nodes(x).getElementsByTagName("description")(0).text
groupurl = group_nodes(x).getElementsByTagName("url")(0).text
response.write("" & groupname & "")
response.write(groupdescription & "")
response.write("Visit This Group
")
next
else
response.write "crash, boom, fail......"
end if
'cleanup
set objXML = nothing
set source = nothing
set group_nodes = nothing
%>