spacer
spacerLokken Logo
spacerLokken Binary Logo
spacer
spacer
spacer
spacer spacer spacer spacer
Use AJAX to get an RSS Feed
Language: ASP  Server Side - JavaScript
Author:   Jon Lokken
Date:   2-26-2008

Description: View Example
This show how you can use AJAX to retrieve an RSS feed from an ASP page.  This page shows the server page JavaScript portion with cross browser compatibility.

<%@ LANGUAGE="JavaScript" %>
<% Response.buffer=true %>
<%
/*
* Get an RSS feed
*/
function getRssFeed(pathOfPage, pathOfStyle, pathOfSave, thisTime){
/* My Date */
var today = new Date();
var month = today.getMonth();
var day = today.getDate();
var year = today.getFullYear();
var hour = today.getHours();
var minute = today.getMinutes();

var objMyFSO= Server.CreateObject("Scripting.FileSystemObject");
var styleFilePath = Server.MapPath(pathOfStyle);
var saveFilePath = Server.MapPath(pathOfSave);
var doesFileExist = objMyFSO.FileExists(styleFilePath);
var doesSaveFileExist = objMyFSO.FileExists(saveFilePath);

// Default Values
var saveFileMinute = 0;
var saveFileHour = 0;
var saveFileDay = 0;
var saveFileMonth = 0;
var saveFileYear = 0;

// Get File Timing
var myTime = "hour";
switch(thisTime){
case "minute":
myTime = "minute";
break;
case "hour":
myTime = "hour";
break;
case "day":
myTime = "day";
break;
case "month":
myTime = "month";
break;
case "year":
myTime = "year"
break;
}

if(doesSaveFileExist){
var saveFile = objMyFSO.getFile(saveFilePath);
var saveFileDate = new Date(saveFile.DateLastModified);
saveFileMinute = saveFileDate.getMinutes();
saveFileHour = saveFileDate.getHours();
saveFileDay = saveFileDate.getDate();
saveFileMonth = saveFileDate.getMonth();
saveFileYear = saveFileDate.getFullYear();
}

if(doesFileExist){
var xmlObj = Server.CreateObject("Msxml2.DomDocument");
var xmlStyleObj = Server.CreateObject("Msxml2.DomDocument");

xmlObj.async = false;
if(!doesSaveFileExist || (myTime == "minute" && ((minute - saveFileMinute) != 0)) || (myTime == "hour" && ((hour - saveFileHour) != 0)) || (myTime == "day" && ((day - saveFileDay) != 0)) || (myTime == "month" && ((month - saveFileMonth) != 0)) || (myTime == "year" && ((year - saveFileYear) != 0))){
try {
xmlObj.setProperty("ServerHTTPRequest", true);
xmlObj.load(pathOfPage);
xmlObj.save(saveFilePath);
} catch(e) {
//Response.Write("Feed Exception:"+e);
}
} else {
xmlObj.load(saveFilePath);
}

xmlStyleObj.async = false;
xmlStyleObj.load(styleFilePath);

xmlObj.transformNodeToObject(xmlStyleObj, Response);

} else {
Response.Write("There was an error reading the rss feed.");
}
}
%>
<% getRssFeed("http://rss.news.yahoo.com/rss/topstories", "/includes/xsl/news2.xsl", "/includes/rssfeeds/news.xml", "minute") %>


Disclaimer:
Feel free to use this code I have written.  I take no responsibility for this code working, imply no warranty as to it's validity, and will not be held liable if you decide to try it.  I am only trying to help out others so they don't have to struggle with the same problems as me...

Associated Sites: Jon Lokken - http://www.jonlokken.com | Amy Lokken - http://www.amylokken.com | Isabel Lokken - http://www.isabellokken.com | Elizabeth Lokken - http://www.elizabethlokken.com | Lokken.us - http://www.lokken.us | Lokken.co - http://www.lokken.co | Lokken Consulting - http://www.lokkenconsulting.com
Author: Jon Lokken - http://www.lokken.net