PDA

View Full Version : XML refresh/display issues


Daniel_Carmel
09-17-2009, 02:45 PM
Hi,

I am attempting to make an RSS news feed for my work, being new to xml, ajax and javascript I'm learning as I go. I am having two problems with my rss feed.

First: I can bring up a table of the rss feed fine once, but then if I update the RSS feed remotely and upload this to the webserver I come across a problem. The rss feed will not display the updated information unless I physically go onto the xml page, and hit refresh.

I have 3 documents to make my page function (xml file, ajax javascript file, html page)

http://www.carmel.org.uk/rss_feed/carmel_rss.xml
http://www.carmel.org.uk/rss_feed/rssajax_carmel.js
http://www.carmel.org.uk/rss_feed/rssajax_carmel.html

If anyone could find the problem with the code I would really appreciate it :)

Second: If anyone can help me with the layout of the rss feed (e.g. css and/or other methods of layout) I would be very greatful.

Also you can e-mail me with any suggestions at:

dmoorhouse@carmel.org.uk

Thanks!

Daniel_Carmel
09-17-2009, 03:13 PM
Source code for carmel_rss.xml:


<?xml version="1.0"?>
<rss version="2.0">
<channel>
<title>Carmel News</title>
<link>http://www.carmel.org.uk</link>
<description>The latest news and updates from Carmel.org.uk</description>
<docs>http://blogs.law.harvard.edu/tech/rss</docs>
<pubDate>Thu, 17 Sep 2009 12:44:16 +0100</pubDate>


<item>
<title>working?</title>
<description>test1</description>
<link>http://www.MyWebSite.com/item.html</link>
<pubDate>Thu, 17 Sep 2009 12:44:16 +0100</pubDate>
</item>

<item>
<title>Sample item2</title>
<description>test1</description>
<link>http://www.MyWebSite.com/item.html</link>
<pubDate>Thu, 17 Sep 2009 12:03:00 +0100</pubDate>
</item>

<item>
<title>Sample item</title>
<description>test3</description>
<link>http://www.MyWebSite.com/item.html</link>
<pubDate>Thu, 17 Sep 2009 12:01:01 +0100</pubDate>
</item>

<item>
<title>Rawr</title>
<description>
<![CDATA[This is to test several things in the rss feed:
test 1: hyperlinks/html - <a href="http://www.carmel.org.uk/">Should Link To Home Page</a>]]></description>
<link>http://www.carmel.org.uk/</link>
<pubDate>Wed, 16 Sep 2009 00:00:00</pubDate>
</item>

</channel>
</rss>


source code for rssajax_carmel.html


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<head>
<link rel="alternate" type="application/rss+xml" title="RSS" href="carmel_rss.xml">
<script language="javascript" src="rssajax_carmel.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Carmel RSS</title>
<style type="text/css">
#chan_items { margin: 20px; }
#chan_items #item { margin-bottom: 10px; }
#chan_items #item #item_title { font-weight: bold; }
#chan_items
</style>
</head>
<body>

<!--A-->
<form name="rssform" onsubmit="getRSS(); return false;">
<select name="rssurl">
<option value="carmel_rss.xml">Test RSS feed</option>
</select>
<input type="submit" value="fetch rss feed" />
</form>
<div id="chan">
<div id="chan_title"></div>
<div id="chan_link"></div>
<div id="chan_description"></div>
<div id="chan_items"></div>
<div id="chan_pubDate"></div>
</div>
</body>
</html>


source code for rssajax_carmel.js



//OBJECTS

//objects inside the RSS2Item object
function RSS2Enclosure(encElement)
{
if (encElement == null)
{
this.url = null;
this.length = null;
this.type = null;
}
else
{
this.url = encElement.getAttribute("url");
this.length = encElement.getAttribute("length");
this.type = encElement.getAttribute("type");
}
}


function RSS2Guid(guidElement)
{
if (guidElement == null)
{
this.isPermaLink = null;
this.value = null;
}
else
{
this.isPermaLink = guidElement.getAttribute("isPermaLink");
this.value = guidElement.childNodes[0].nodeValue;
}
}


function RSS2Source(souElement)
{
if (souElement == null)
{
this.url = null;
this.value = null;
}
else
{
this.url = souElement.getAttribute("url");
this.value = souElement.childNodes[0].nodeValue;
}
}


//object containing the RSS 2.0 item
function RSS2Item(itemxml)
{
//required
this.title;
this.link;
this.description;

//optional vars
this.pubDate;


var properties = new Array("title", "link", "description", "pubDate");
var tmpElement = null;
for (var i=0; i<properties.length; i++)
{
tmpElement = itemxml.getElementsByTagName(properties[i])[0];
if (tmpElement != null)
eval("this."+properties[i]+"=tmpElement.childNodes[0].nodeValue");
}

/*this.category = new RSS2Category(itemxml.getElementsByTagName("category")[0]);
this.enclosure = new RSS2Enclosure(itemxml.getElementsByTagName("enclosure")[0]);
this.guid = new RSS2Guid(itemxml.getElementsByTagName("guid")[0]);
this.source = new RSS2Source(itemxml.getElementsByTagName("source")[0]);*/
}

//objects inside the RSS2Channel object
/*
function RSS2Category(catElement)
{
if (catElement == null)
{
this.domain = null;
this.value = null;
}
else
{
this.domain = catElement.getAttribute("domain");
this.value = catElement.childNodes[0].nodeValue;
}
}*/

//object containing RSS image tag info
function RSS2Image(imgElement)
{
if (imgElement == null)
{
this.url = null;
this.link = null;
this.width = null;
this.height = null;
this.description = null;
}
else
{
imgAttribs = new Array("url","title","link","width","height","description");
for (var i=0; i<imgAttribs.length; i++)
if (imgElement.getAttribute(imgAttribs[i]) != null)
eval("this."+imgAttribs[i]+"=imgElement.getAttribute("+imgAttribs[i]+")");
}
}

//object containing the parsed RSS 2.0 channel
function RSS2Channel(rssxml)
{
//required
this.title;
this.link;
this.description;
this.pubDate;
//array of RSS2Item objects
this.items = new Array();

//optional vars


//optional objects
this.category;
this.image;

var chanElement = rssxml.getElementsByTagName("channel")[0];
var itemElements = rssxml.getElementsByTagName("item");

for (var i=0; i<itemElements.length; i++)
{
Item = new RSS2Item(itemElements[i]);
this.items.push(Item);
//chanElement.removeChild(itemElements[i]);
}

var properties = new Array("title", "link", "description", "pubDate");
var tmpElement = null;
for (var i=0; i<properties.length; i++)
{
tmpElement = chanElement.getElementsByTagName(properties[i])[0];
if (tmpElement!= null)
eval("this."+properties[i]+"=tmpElement.childNodes[0].nodeValue");
}

/*this.category = new RSS2Category(chanElement.getElementsByTagName("category")[0]);
this.image = new RSS2Image(chanElement.getElementsByTagName("image")[0]);*/
}

//PROCESSES

//uses xmlhttpreq to get the raw rss xml
function getRSS()
{
//call the right constructor for the browser being used
if (window.ActiveXObject)
xhr = new ActiveXObject("Microsoft.XMLHTTP");
else if (window.XMLHttpRequest)
xhr = new XMLHttpRequest();
else
alert("not supported");

//prepare the xmlhttprequest object
xhr.open("GET",document.rssform.rssurl.value,true);
xhr.setRequestHeader("Cache-Control", "no-cache");
xhr.setRequestHeader("Pragma", "no-cache");
xhr.onreadystatechange = function() {
if (xhr.readyState == 4)
{
if (xhr.status == 200)
{
if (xhr.responseText != null)
processRSS(xhr.responseXML);
else
{
alert("Failed to receive RSS file from the server - file not found.");
return false;
}
}
else
alert("Error code " + xhr.status + " received: " + xhr.statusText);
}
}

//send the request
xhr.send(null);
}

//processes the received rss xml
function processRSS(rssxml)
{
RSS = new RSS2Channel(rssxml);
showRSS(RSS);
}

//shows the RSS content in the browser
function showRSS(RSS)
{
//default values for html tags used
//var imageTag = "<img id='chan_image'";
var startItemTag = "<div id='item'>";
var startTitle = "<div id='item_title'>";
var startLink = "<div id='item_link'>";
var startDescription = "<div id='item_description'>";
var endTag = "</div>";

//populate channel data
var properties = new Array("title","link","description","pubDate");
for (var i=0; i<properties.length; i++)
{
eval("document.getElementById('chan_"+properties[i]+"').innerHTML = ''");
curProp = eval("RSS."+properties[i]);
if (curProp != null)
eval("document.getElementById('chan_"+properties[i]+"').innerHTML = curProp");
}

//show the image
/*document.getElementById("chan_image_link").innerHTML = "";
if (RSS.image.src != null)
{
document.getElementById("chan_image_link").href = RSS.image.link;
document.getElementById("chan_image_link").innerHTML = imageTag
+" alt='"+RSS.image.description
+"' width='"+RSS.image.width
+"' height='"+RSS.image.height
+"' src='"+RSS.image.url
+"' "+"/>";
}*/

//populate the items
document.getElementById("chan_items").innerHTML = "";
for (var i=0; i<RSS.items.length; i++)
{
item_html = startItemTag;
item_html += (RSS.items[i].title == null) ? "" : startTitle + RSS.items[i].title + endTag;
item_html += (RSS.items[i].link == null) ? "" : startLink + RSS.items[i].link + endTag;
item_html += (RSS.items[i].description == null) ? "" : startDescription + RSS.items[i].description + endTag;
item_html += endTag;
document.getElementById("chan_items").innerHTML += item_html;
}

//we're done
//document.getElementById("chan").style.visibility = "visible";
return true;
}

var xhr;

orca8767
09-18-2009, 09:03 PM
It's because the browser is caching the XML file. So, you need to make the server send a header that tells it to not cache the file. Or, you can use JavaScript to tell it this. JQuery's AJAX object has a built-in feature for disabling caching of the requested file.

Daniel_Carmel
09-18-2009, 09:53 PM
Thanks for the reply, I had an idea it might be the cache but despite my best efforts it's still not quite working, could you give a code example of how I can use the javascript to stop the caching?

Thanks in advance :)

orca8767
09-20-2009, 12:50 AM
You could generate a random string and append it to the end of the file you're requesting with a question mark (blah.xml?jaseiov7ybtnaeib)

Here's a function to generate a random string (In JavaScript):
function randomString() {
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var string_length = 8;
var string = '';
for (var i = 0; i <= string_length; i++) {
var rnum = Math.floor(Math.random() * chars.length);
string += chars.substring(rnum,rnum+1);
}
return string;
}


Usage:
var resource_to_request = 'blah.xml?'+ randomString();