Posted By

chrisaiv on 02/22/09


Tagged

js rss


Versions (?)

Who likes this?

7 people have marked this snippet as a favorite

fabianwizenfeld
umang_nine
rchk
rdavidson
wirenaught
tof
Beppoi


JS: Parse RSS Feed


 / Published in: JavaScript
 

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

  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
Posted By: kideh on November 11, 2010

By any chance could you post the source for getfeed.php?

Posted By: kideh on November 11, 2010

By any chance could you post the source for getfeed.php?

Posted By: chrisaiv on November 17, 2010

Hey kideh, Here's the PHP script. http://snipplr.com/view/12467/php-capture-rss-feed/

You need to login to post a comment.