Because RSS channels have recently been used in projects, it took some time today to write a JS class to parse RSS channels. To put it bluntly, it is just JavaScript parsing XML files. my JS class provides parsing XML to return JSON objects, which facilitates the operation of result sets. currently, only the parsing of rss2.0 specifications is supported. However, my Js class is also very simple. I only need to obtain the title and link fields, and the rest will not work, therefore, both the rss1.0 and rss0.9 specifications can be supported. Please try again later. in addition, I originally wanted to directly access the RSS feed address on the web page, but I forgot That Ajax does not allow cross-origin access, therefore, you can only write a program in the background to obtain the RSS content, and then generate it on the local disk. Finally, you can directly access the local XML file on the web page.
Source code of rssreader:
Jsscript code
- 01 /**
- 02 * @ title: RSS channel reading and parsing class.
- 03 * @ Author: Iron wood box
- 04 * @ modify:
- 05 */
- 06
- 07 function rssreader (){};
- 08 rssreader. Prototype = {
- 09 /**
- 10 * obtain and parse RSS. This method integrates the getrss and parsexmltojson methods, and the final callback function value is a JSON object.
- 11 */
- 12 getandparse: function (/* string */_ URL,/* function */callback ){
- 13 var parsexml = This. parserss;
- 14 This. getrss (_ URL, function (rssxml ){
- 15 var JSON = parsexml (rssxml );
- 16 callback (JSON );
- 17 });
- 18 },
- 19
- 20 /**
- 21 * Get the XML file content of the RSS channel and return it as an XML object.
- 22 * @ Param _ URL the URL of the RSS to be obtained
- 23 * @ Param callback: callback method after successful or failed retrieval. The parameter is an XML Object of RSS content (null is returned if failed)
- 24 */
- 25 getrss: function (/* string */_ URL,/* function */callback ){
- 26 new Ajax. Request (
- 27 _ URL,
- 28 {
- 29 method: 'get ',
- 30 oncomplete: function (XMLHTTP ){
- 31 var rssxml = XMLHTTP. responsexml;
- 32 If (rssxml = NULL) Alert ('invalid RSS channel content, cannot be parsed! ');
- 33 callback (rssxml );
- 34}
- 35}
- 36 );
- 37 },
- 38
- 39 /**
- 40 * convert the XML content of the RSS feed into a JSON object and return it.
- 41 */
- 42 parserss: function (/* object */rssxml ){
- 43 try {
- 44 var RSS = rssxml. getelementsbytagname ('rss ');
- 45 var channel = rssxml. getelementsbytagname ('channel ');
- 46 If (! RSS |! Channel | RSS. length! = 1 | channel. Length <1 ){
- 47 alert ('invalid RSS format: No RSS node or channel node! ');
- 48 return NULL;
- 49}
- 50 var version = RSS. Item (0). getattribute ('version ');
- 51 if (! Version ){
- 52 alert ('invalid RSS format: No RSS version is specified! ');
- 53 return NULL;
- 54}
- 55 if (version = '2. 0') return rssreader. parserss2_0 (Channel. Item (0 ));
- 56
- 57 alert ('invalid RSS format: unable to determine the RSS version! ');
- 58 return NULL;
- 59} catch (Ex ){
- 60 alert ('error occurred when parsing RSS content: '+ ex. Message );
- 61 return NULL;
- 62}
- 63}
- 64 };
- 65
- 66 /**
- 67 * Static Parsing Method in rss2.0 format. The returned result is a JSON object.
- 68 * The returned JSON format is:
- 69 * {"items": [{"title": "title", "Link": "connection address "},
- 70 * {"title": "title", "Link": "connection address "},
- 71 * {"title": "title", "Link": "connection address "}
- 72 *]
- 73 *}
- 74 * usage: (assume that the final returned Object Name Is JSON ):
- 75 * JSON. Items. Length-get the total number of items
- 76 * JSON. items [I]-object for obtaining each piece of information (0 <= I <JSON. Items. length)
- 77 * JSON. items [I]. Title-title text for obtaining a single message
- 78 * JSON. items [I]. Link-connection address for obtaining a single message
- 79 */
- 80 rssreader. parserss2_0 = function (/* object */channel ){
- 81 var items = channel. getelementsbytagname ('item ');
- 82 var JSON = '{"items ":[';
- 83 for (VAR I = 0; I <items. length; I ++ ){
- 84 var item = items. item (I );
- 85 VaR _ Title = item. getelementsbytagname ('title') [0];
- 86 VaR _ link = item. getelementsbytagname ('link') [0];
- 87 If (! _ Title. haschildnodes () |! _ Link. haschildnodes () continue;
- 88 JSON + = '{"title": "' + _ title. firstchild. nodevalue. gsub (/"/, "'") + '",';
- 89 JSON + = '"Link": "' + _ link. firstchild. nodevalue. gsub (/"/, "'") + '"}';
- 90 if (I <items. Length-1) JSON + = ',';
- 91}
- 92 JSON + = ']}';
- 93 // alert (JSON );
- 94 return eval ('+ JSON + ')');
- 95 };