JS: Parse RSS Feed


/ Published in: JavaScript
Save to your folder(s)

Simple Javascript that will get a feed and parse it using Ajax


Copy this code and paste it in your HTML
  1. function SendAjax(url, callbackFunction) {
  2. if (window.XMLHttpRequest) {
  3. var request = new XMLHttpRequest();
  4. } else {
  5. var request = new ActiveXObject("Microsoft.XMLHTTP");
  6. }
  7.  
  8. request.open("POST", url, true);
  9. request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  10.  
  11. request.onreadystatechange = function() {
  12. if (request.readyState == 4 && request.status == 200) {
  13. if (request.responseText) {
  14. ReceiveAjax(request.responseText, callbackFunction);
  15. }
  16. }
  17. }
  18.  
  19. request.send(null);
  20. }
  21.  
  22. function ReceiveAjax(response, callbackFunction) {
  23. if (window.ActiveXObject) {
  24. var doc = new ActiveXObject("Microsoft.XMLDOM");
  25. doc.async = "false";
  26. doc.loadXML(response);
  27. } else {
  28. var parser = new DOMParser();
  29. var doc = parser.parseFromString(response,"text/xml");
  30. }
  31.  
  32. callbackFunction(doc.documentElement);
  33. }
  34.  
  35. function ValueFromTagName(item, tagname) {
  36. var val = item.getElementsByTagName(tagname);
  37. return val[0].firstChild.nodeValue;
  38. }
  39.  
  40. function ParseData(data) {
  41. var items = data.getElementsByTagName('item');
  42. var output = '<ul>';
  43.  
  44. for (var i = 0; i < items.length; ++i) {
  45. var title = ValueFromTagName(items[i], 'title');
  46. var link = ValueFromTagName(items[i], 'link');
  47.  
  48. output += '<li><strong><a href ="' + link + '">' + title + '</strong> ' + '</li>';
  49. }
  50.  
  51. output += '</ul>';
  52.  
  53. var RSSOutput = document.getElementById('RSSOutput');
  54. RSSOutput.innerHTML = output;
  55. }
  56.  
  57. function LoadFeed( url ) {
  58. var GetFeed = url;
  59. SendAjax('getfeed.php?url=' + escape(GetFeed.value), ParseData);
  60. }
  61. LoadFeed( "http://del.icio.us/rss/twelvetone" );

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.