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

Parsing ATOM feeds

Wed Aug 22, 2012 1:50 pm

In this article we show a small example for parsing atom feeds using javascript code. Atom format was developed as an alternative to RSS. The innovation of Atom design was based on the believe that RSS had limitations and flaws—such as lack of on-going innovation and its necessity to remain backward compatible. Following is an example of ATOM format :
Code:

<?xml version="1.0" encoding="utf-8"?>
 
<feed xmlns="http://www.w3.org/2005/Atom">
 
        <title>Title here (Feed Title)</title>
        <subtitle>If there a subtitle</subtitle>
        <link href="http://codemiles.com/feed/" rel="self" />
        <link href="http://codemiles.com/" />
        <id>urn:uuid:5ce14e53-a349-1455-c433-4305315cccc6</id>
        <updated>2012-11-11T18:10:01Z</updated>
 
 
        <entry>
                <title>Parsing ATOM Feeds</title>
                <link href="http://codemiles.com/2012/11/11/atom03" />
                <link rel="alternate" type="text/html" href="http://codemiles.com/2012/11/11/atom03.html"/>
                <link rel="edit" href="http://codemiles.com/2012/11/11/atom03/edit"/>
                <id>urn:uuid:14444ccc-13ec-3cc2-142c-13ac4cec5c3c</id>
                <updated>2012-11-11T18:10:02Z</updated>
                <summary>Some text.</summary>
                <author>
                      <name>msi_333</name>
                      <email>[email protected]</email>
                </author>
        </entry>
 
</feed>


Following is an code snippet for parsing XML ATOM format using JavaScript ( Using Dojo JavaScript Toolkit) :
Code:

<script type="text/javascript">
dojo.addOnLoad(function() {
      var htmlDivID = "contentDiv";
      
      
// Parse ATOM feed
      dojo.xhrGet({
         url: "myFeed.xml",
         preventCache: true,
         handleAs: "xml",
          load: function(xmlDoc, ioArgs){         
             var i 
= 0;
             var htmlOutputContent = "";
          
             var node 
= xmlDoc.getElementsByTagName("feed").item(0);
         
             if 
(node == null) {
                 console.debug("ERROR: Error in parsing ATOM Feeds XML format.");                     
                 return
;
             }
         
             var NumOfFeedsEntry 
= node.getElementsByTagName("entry").length;
         
             for 
(= 0; i < NumOfFeedsEntry; ++i) {
                var entry = node.getElementsByTagName('entry').item(i);
                 
                var title 
= entry.getElementsByTagName('title').item(0).firstChild.data;
                 var published = entry.getElementsByTagName('published').item(0).firstChild.data;            
                 var summary 
= entry.getElementsByTagName('summary').item(0).firstChild.data;
                 var link = entry.getElementsByTagName('link').item(0).getAttribute("href");
                 
                htmlOutputContent 
+= '<hr><p><a target="_blank" href="' + link +'">' + title + '</a><br/>' + 
                          
'<span class="smaller">' + published + '</span><br/>' + 
                          summary 
+
                          '</p>';
             }
             
             document
.getElementById(htmlDivID).innerHTML = htmlOutputContent;             
         
},
         error: function(error, ioArgs){                 
             dojo
.byId(htmlDivID).innerHTML = "Error In Loading and Parsing The Atom Feeds";
             console.debug("ATOM URL CONTENT PASRING ERROR (DEBUG): ", error, ioArgs);             
         
}
    });
});
    
</script>

<div id="contentDiv"></div>


Dojo is open source toolkit developed to ease the JavaScript\Ajax applications, for more information about Dojo :
Code:
http://en.wikipedia.org/wiki/Atom_%28standard%29




Post a reply
  Related Posts  to : Parsing ATOM feeds
 Parsing and Reading RSS feeds     -  
 Load RSS feeds and parse it with JQuery     -  
 Parsing a Processing Instruction     -  
 Latest XML parsing/memory usage benchmark     -  

Topic Tags

JavaScript ATOM