Switch to full style
HTML/DHTML/JAVASCRIPT/AJAX Technology Tutorials Written By Members.
Post a reply

Parsing and Reading RSS feeds

Wed Aug 22, 2012 2:21 pm

In this small article we are reading RSS using JavaScript code, RSS stands for (Rich Site Summary), RSS has its own format for providing information about the latest websites content updates, such as blogs/forums/wikis articles, images and videos. Many versions of RSS have many developed, currently we see the format of RSS 2.0 at following XML format :
Code:



<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
        <title>RSS Feeds Title</title>
        <description>Feeds by RSS example</description>
        <link>http://www.codemiles.com/rss.html</link>
        <lastBuildDate>Mon, 06 Sep 2012 00:02:00 +0000 </lastBuildDate>
        <pubDate>Mon, 06 Sep 2012 11:13:00 +0000 </pubDate>
        <ttl>1800</ttl>
 
        <item>
                <title>RSS feeds parsing</title>
                <description>How to parse RSS in javascript....</description>
                <link>http://www.codemiles.com/javascriptRSS.html</link>
                <guid>unique string per item</guid>
                <pubDate>Mon, 06 Sep 2012 11:11:00 +0000 </pubDate>
        </item>
 
</channel>
</rss>


In this article we parse RSS XML using JavaScript, we use the Dojo free toolkit for javascript/ajax applications :
Code:

<script type="text/javascript">
dojo.addOnLoad(function() {
      var divID = "divShow";
      
      
// Parse Rss Feed
      dojo.xhrGet({
         url: "rssFile.xml",
         preventCache: true,
         handleAs: "xml",
         load: function(xmlDoc, ioArgs){         
             var i 
= 0;
             var htmlReturn = "";
          
             var mainNode 
= xmlDoc.getElementsByTagName("channel").item(0);
         
             if 
(mainNode == null) {
                 console.debug("Error in RSS format ...");                           
                 return
;
             }
         
             var NumberOfItems 
= mainNode.getElementsByTagName("item").length;
             
             for 
(= 0; i < NumberOfItems; ++i) {
                var entry = mainNode.getElementsByTagName('item').item(i);
                 
                var title 
= entry.getElementsByTagName('title').item(0).firstChild.data;
                 var published = entry.getElementsByTagName('pubDate').item(0).firstChild.data;            
                 var description 
= entry.getElementsByTagName('description').item(0).firstChild.data;
                 var link = entry.getElementsByTagName('link').item(0).firstChild.data;
                 
                htmlReturn 
+= '<hr><p><a target="_blank" href="' + link +'">' + title + '</a><br/>' + 
                          
'<span class="smaller">' + published + '</span><br/>' + 
                          description 
+
                          '</p>';
             }
                 
             document
.getElementById(divID).innerHTML = htmlReturn;             
       
},
       error: function(error, ioArgs){             
             dojo
.byId(divID).innerHTML = "Failed To Load RSS Feeds";
             console.debug("failed xhrGet for Rss URL: ", error, ioArgs);                   
         
}      
    
});
});
    
</script>

<div id="divShow"></div>


As you can see the code ,we read each items (node) and its children (Title,Description ... etc) .

For more information about RSS
Code:
http://en.wikipedia.org/wiki/RSS


For more information about DOJO toolkit :
Code:
http://en.wikipedia.org/wiki/Dojo_Toolkit




Post a reply
  Related Posts  to : Parsing and Reading RSS feeds
 Parsing ATOM feeds     -  
 Load RSS feeds and parse it with JQuery     -  
 Parsing a Processing Instruction     -  
 Latest XML parsing/memory usage benchmark     -  
 Reading the all file in php     -  
 Java Object Reading     -  
 Reading a Specific Character in php     -  
 Reading email in Python     -  
 Reading file with integers     -  
 Multithreaded File Reading     -  

Topic Tags

JavaScript RSS