Parse RSS in JS

Source: Internet
Author: User

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
  1. 01 /**
  2. 02 * @ title: RSS channel reading and parsing class.
  3. 03 * @ Author: Iron wood box
  4. 04 * @ modify:
  5. 05 */
  6. 06
  7. 07 function rssreader (){};
  8. 08 rssreader. Prototype = {
  9. 09 /**
  10. 10 * obtain and parse RSS. This method integrates the getrss and parsexmltojson methods, and the final callback function value is a JSON object.
  11. 11 */
  12. 12 getandparse: function (/* string */_ URL,/* function */callback ){
  13. 13 var parsexml = This. parserss;
  14. 14 This. getrss (_ URL, function (rssxml ){
  15. 15 var JSON = parsexml (rssxml );
  16. 16 callback (JSON );
  17. 17 });
  18. 18 },
  19. 19
  20. 20 /**
  21. 21 * Get the XML file content of the RSS channel and return it as an XML object.
  22. 22 * @ Param _ URL the URL of the RSS to be obtained
  23. 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. 24 */
  25. 25 getrss: function (/* string */_ URL,/* function */callback ){
  26. 26 new Ajax. Request (
  27. 27 _ URL,
  28. 28 {
  29. 29 method: 'get ',
  30. 30 oncomplete: function (XMLHTTP ){
  31. 31 var rssxml = XMLHTTP. responsexml;
  32. 32 If (rssxml = NULL) Alert ('invalid RSS channel content, cannot be parsed! ');
  33. 33 callback (rssxml );
  34. 34}
  35. 35}
  36. 36 );
  37. 37 },
  38. 38
  39. 39 /**
  40. 40 * convert the XML content of the RSS feed into a JSON object and return it.
  41. 41 */
  42. 42 parserss: function (/* object */rssxml ){
  43. 43 try {
  44. 44 var RSS = rssxml. getelementsbytagname ('rss ');
  45. 45 var channel = rssxml. getelementsbytagname ('channel ');
  46. 46 If (! RSS |! Channel | RSS. length! = 1 | channel. Length <1 ){
  47. 47 alert ('invalid RSS format: No RSS node or channel node! ');
  48. 48 return NULL;
  49. 49}
  50. 50 var version = RSS. Item (0). getattribute ('version ');
  51. 51 if (! Version ){
  52. 52 alert ('invalid RSS format: No RSS version is specified! ');
  53. 53 return NULL;
  54. 54}
  55. 55 if (version = '2. 0') return rssreader. parserss2_0 (Channel. Item (0 ));
  56. 56
  57. 57 alert ('invalid RSS format: unable to determine the RSS version! ');
  58. 58 return NULL;
  59. 59} catch (Ex ){
  60. 60 alert ('error occurred when parsing RSS content: '+ ex. Message );
  61. 61 return NULL;
  62. 62}
  63. 63}
  64. 64 };
  65. 65
  66. 66 /**
  67. 67 * Static Parsing Method in rss2.0 format. The returned result is a JSON object.
  68. 68 * The returned JSON format is:
  69. 69 * {"items": [{"title": "title", "Link": "connection address "},
  70. 70 * {"title": "title", "Link": "connection address "},
  71. 71 * {"title": "title", "Link": "connection address "}
  72. 72 *]
  73. 73 *}
  74. 74 * usage: (assume that the final returned Object Name Is JSON ):
  75. 75 * JSON. Items. Length-get the total number of items
  76. 76 * JSON. items [I]-object for obtaining each piece of information (0 <= I <JSON. Items. length)
  77. 77 * JSON. items [I]. Title-title text for obtaining a single message
  78. 78 * JSON. items [I]. Link-connection address for obtaining a single message
  79. 79 */
  80. 80 rssreader. parserss2_0 = function (/* object */channel ){
  81. 81 var items = channel. getelementsbytagname ('item ');
  82. 82 var JSON = '{"items ":[';
  83. 83 for (VAR I = 0; I <items. length; I ++ ){
  84. 84 var item = items. item (I );
  85. 85 VaR _ Title = item. getelementsbytagname ('title') [0];
  86. 86 VaR _ link = item. getelementsbytagname ('link') [0];
  87. 87 If (! _ Title. haschildnodes () |! _ Link. haschildnodes () continue;
  88. 88 JSON + = '{"title": "' + _ title. firstchild. nodevalue. gsub (/"/, "'") + '",';
  89. 89 JSON + = '"Link": "' + _ link. firstchild. nodevalue. gsub (/"/, "'") + '"}';
  90. 90 if (I <items. Length-1) JSON + = ',';
  91. 91}
  92. 92 JSON + = ']}';
  93. 93 // alert (JSON );
  94. 94 return eval ('+ JSON + ')');
  95. 95 };

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.