Wednesday, July 29, 2009

Parsing RSS feeds with XMLHttpRequest Object

You can parse RSS feeds with thehelp of XMLHttpRequest Object means using javascript or ajax

or jquery . what you have to do in this case is ones you ot the Xml Object you can traverse it

via DOM (Document Object Model).

here is some example which shows how u can done xml Parsing


<script type="text/javascript" language="javascript">
var XMLObjString = '\
<rss version="2.0">\
<channel>\
<title>Test</title>\
<link>http://www.xyz.com/</link>\
<description>RSS Feed</description>\
<language>EN</language>\
<image>\
<url>http://www.xyz.com/images/test.gif</url>\
</image>\
<category>JavaScript</category>\
<item>\
<title>5:20 PM 7/28/2009</title>\
<link>http://www.xyz.com/index.html</link>\
<description>
This is index page | Message at : 5:24 PM 7/28/2009
</description>\
</item>\
</channel>\
</rss>';


// Now convert string to XML object

var xmlobj = (new DOMParser()).parseFromString(XMLObjString, "text/xml");

// set the reference to the root-element "rss"
var root = xmlobject.getElementsByTagName('rss')[0];

// like that you can set reference to "channel" element
var channels = root.getElementsByTagName("channel");

// find all the "item" tags in the channel
var items = channels[0].getElementsByTagName("item");

// in the "item" find the Description to get the Desc also.
var descriptions = items[0].getElementsByTagName("description");


// you can also get the actual description as string
var desc = descriptions[0].firstChild.nodeValue;

// now split the string - description is element

var Descarray = desc.split("|");
var Message = Descarray[1];

// now we have the RSS data we want as strings
alert(Message);

</script>


Now you can get the data at "alert(Message); tag you can use it whenever you want

No comments:

Post a Comment