Using XML Web Services in Traditional ASP - Putting it all Together
(Page 4 of 4 )
Now we should have all the building blocks we need to actually output the contents of the XML file via the XSL to the browser. We will do this in a very simple traditional ASP function, using the MSXML component library. Make sure the web server you are running the code from has the MSXML component installed.
The Function
This is the actual function you need to put in your ASP page.
<%
Function getFlickrXML(flickrUrl, xslSheet)
dim styleFile
dim source1, style
styleFile = Server.MapPath(xslSheet)
dim xmlhttp
set xmlhttp = Server.CreateObject("Microsoft.XMLHTTP")
xmlhttp.Open "GET", flickrUrl, false
xmlhttp.Send
set source1 = Server.CreateObject("Microsoft.XMLDOM")
source1.async = false
source1.loadxml(xmlhttp.ResponseText)
set style = Server.CreateObject("Microsoft.XMLDOM")
style.async = false
style.load(styleFile)
Response.Write source1.transformNode(style)
set source1 = nothing
set style = nothing
set xmlhttp = nothing
End Function
%>
This function should be very open ended, and work with any service, not just Flickr as we are passing it both the URL from which to get the information, and the style sheet to use.
All it does is create an XMLHTTP object and get the data from the URL you passed into the function. It then loads the XSL style file you send it into a XMLDOM object.
Then, using response.write and the transformNode function, it writes out the content of the XML file in a format based on what's in the XSL file.
To call the function, pass it the URL and the name of the XSL. In our example your actual code would look something like this.
<table width="100%" border="0">
<%= getFlickrXML("http://www.flickr.com/services/rest/?
method=flickr.photosets.getPhotos&api_key=[your_api_key&photoset_id=
[your_set_id]","flickr-lists.xsl")%></td>
</table>
Save your ASP and XSL file onto your web server in the same folder, browse to the ASP file and if all has worked, you should see all the photos from your set, in a table 7 columns across, all with clickable links to the photo's page on Flickr.com
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |