To implement a JavaScript class that reads RSS 2.0

Source: Internet
Author: User
Tags object header implement json return version access
Javascript|rss

These days in learning some JavaScript things, new start, very poor, had to write some small example practicing. Well, put it back here for backup.

This is a class that can be used to read RSS 2.0 information and turn what you read into a JavaScript object. And the Internet can be found in comparison to the code, I this very rough, the package is not necessarily reasonable, anyway, as a practice, to achieve the goal

First of all, of course, to understand the structure of RSS 2.0, its most basic skeleton is as follows:


<?xml version= "1.0"?>
<rss version= "2.0" >
<channel>
<title></title>
<link></link>
<description></description>
<item>
<author></author>
<title></title>
<description></description>
<link></link>
<pubDate></pubDate>
</item>
</channel>
</rss>


Well, this is not the most standard RSS, but the simplest RSS. The actual RSS standard can look http://blogs.law.harvard.edu/tech/rss, still very complicated!

Then, to make it easier to read the contents of each node in the RSS XML, a function that turns directly from XML into a JavaScript object, well, this function can get the data directly from the JavaScript object's structure. Oh, something like ORM.


function Getxmldata (XML, obj) {
For (o in obj) {
if ("object" = = typeof (Obj[o])) {
Getxmldata (Xml.getelementsbytagname (o), Obj[o]);
} else {
Obj[o] = Getxmlnodetext (Xml.getelementsbytagname (O). Item (0));
}
}
}


Oh, yes, there's a function Getxmlnodetext not written, this is the function to read the XML node content. It really sucks, ie and gecko in the XML DOM interface is too not unified, even to get a node of the content is different, ie is. Text,gecko is. Textcontent. No way, I had to write a function to encapsulate ...


function Getxmlnodetext (node) {
var undefined;

if (undefined!== node.text) {
return node.text;
else if (undefined!== node.textcontent) {
return node.textcontent;
}

return undefined;
}


The next step is to write this RSS class. To separate the data from the control, the RSS class does not contain any remote access code, only to retrieve all the data from the input XML node. Oh, yes, this class has almost completely failed to consider the problem of exception protection ...


function RssReader (XML) {
/** Retrieve The information of the tag "channel" in RSS.
@param [in] channel the <channel> node, which are the direct child of <rss>
@return Header object, including title, Link and description.
*/
function GetHeader (channel) {
var Header = {
title:0,
link:0,
description:0
};

Getxmldata (channel, header);

return header;
}

/** Retrieve The information of the tag "item" in RSS.
@param [in] Item the <item> node, which are the direct child of <channel>
@return Item object, including author, title, Link, description and pubdate.
*/
function GetItem (item) {
var info = {
author:0,
title:0,
link:0,
description:0,
pubdate:0
};

Getxmldata (item, info);

return info;
}

var rss = xml.documentelement;
var channel = Rss.getelementsbytagname ("channel"). Item (0);

This.header = GetHeader (channel);
This.items = [];

var item = channel.getelementsbytagname ("item");

for (var i = 0; i < item.length; i++) {
This.items.push (GetItem (Item.item (i)));
}
}


All right, in the end! Of course it is to start accessing the remote RSS feed. Well, this thing, of course, with the use of XMLHTTP, the relevant content has been very mature, I also almost only CP a bit, so of course, this omitted.

After completion, write a summary and experience:

It's easy to do ORM in JavaScript, or even take it for granted. This is because for any object (known as obj), its attributes (assumed to be called attrib) are inherently two ways of accessing, Obj.attrib and obj["attrib". Plus for. In the powerful feature, from XML to JavaScript object is very easy to invincible.
The process from XML to JavaScript objects can also be viewed as a process from XML to JSON, which in turn is easy to implement. Well, if you getxmldata, you can get an XML map directly to the JSON solution, which looks pretty good.
At first I did not understand the security of XMLHTTP, so the opposite is more romantic ... Then I glanced at the Ajax in action to get a glimpse of the details ... Originally, the XMLHTTP object can not easily access any Web site, can only access the other pages under the current site. If you do this, the browser will make an error. Well, I've also tried, if the server redirect to other url,xmlhttp can automatically access other URLs, but in the security above or follow the above principles.
Well, JavaScript stuff, it's really fun.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.