Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Czytnik RSS
Forum PHP.pl > Forum > XML, AJAX
mwolodzko
Znalazłem poniższy skrypt w sieci. Problem polega na tym: w pliku XML znajdą się jakieś artykuły nawet do tysiąca znaków. Jak zrobić coś takiego żeby pokazywał się tytuł artykułu oraz wstęp jakieś 50-100 znaków. A po kliknięciu w artykuł czy link "więcej", w nowym oknie pokazywałaby się pełna treść artykułu.

  1. <head>
  2. <script language="javascript" src="rss.js"></script>
  3. <script language="javascript">
  4. window.onload = function()
  5. {
  6. ReadRSS('cnn_tech_rss.xml','rssBodyTemplate','rssTitleTemplate');
  7. }
  8. </script>
  9. </head>
  10. <body>
  11. <div id="rssTitleTemplate">
  12. (::Title::)<br/>
  13. <a href="(::Link::)"><h1>(::Description::)</h1></a>
  14. </div>
  15. <div id="rssBodyTemplate">
  16. <a href="(::Link::)"><b>(::Title::)</b></a>
  17. <b>(::Pubdate::)</b>
  18. <br/>
  19. <font size="-1">(::Description::)</font>
  20. <hr noshade/>
  21. <br/>
  22. </div>
  23. </body>
  24. </html>

  1. function Replace(totalValue,oldValue,newValue)
  2. {
  3. while(totalValue.indexOf(oldValue) > -1)
  4. totalValue=totalValue.replace(oldValue,newValue);
  5. return totalValue;
  6. }
  7.  
  8. /* Get XML Node
  9. (Parameters) TagName:'XML Element' node:'Element row number' */
  10.  
  11. function getNode(TagName, node)
  12. {
  13. var currentNode = (node == null) ? xmlDoc.getElementsByTagName(TagName) :
  14. items[node].getElementsByTagName(TagName);
  15. if(currentNode.length > 0)
  16. return currentNode[0].firstChild.nodeValue;
  17. }
  18.  
  19. /* Load XML Object
  20. (Parameters) rssFeed:'RSS File' Body:'Layer for RSS Body' Title:'Layer for RSS Title' */
  21.  
  22. function ReadRSS(rssFeed, Body, Title)
  23. {
  24. rssTitle = document.getElementById(Title);
  25. rssBody = document.getElementById(Body);
  26.  
  27. try
  28. {
  29. if (document.all)
  30. {
  31. var errorHappendHere = "Check Browser and security settings";
  32. xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  33. }
  34. else
  35. {
  36. var errorHappendHere = "Apparently one cant read remote xml via firefox, please copy the file to your server";
  37. xmlDoc = document.implementation.createDocument("","",null);
  38. }
  39.  
  40. xmlDoc.async=false;
  41. xmlDoc.load(rssFeed);
  42.  
  43. items=xmlDoc.getElementsByTagName('item');
  44. SetRSSTemplates();
  45. }
  46.  
  47. catch(e)
  48. {
  49. rssTitle.innerHTML = 'Error occured';
  50. rssBody.innerHTML = 'Thrown Error:'+e.message+"<br/>Note: "+errorHappendHere;
  51. }
  52. }
  53.  
  54. /* Set HTML Template
  55. Did it this way to make the look and feel of the feed easy customizable, dont like mixing
  56. layout with code. */
  57.  
  58. function SetRSSTemplates()
  59. {
  60. if (rssBody)
  61. {
  62. var buffer = "";
  63. for(var i=0; i< items.length; i++)
  64. {
  65. var output = (document.all) ? Replace(rssBody.innerHTML,"(::Link::)",getNode('link',i))
  66. : Replace(rssBody.innerHTML,"%28::Link::%29",getNode('link',i));
  67. output = Replace(output,"(::Title::)",getNode('title',i));
  68. output = Replace(output,"(::Pubdate::)",getNode('pubDate',i));
  69. output = Replace(output,"(::Description::)",getNode('description',i));
  70. buffer+=output;
  71. }
  72. rssBody.innerHTML = buffer;
  73. }
  74.  
  75. if (rssTitle)
  76. {
  77. var output = Replace(rssTitle.innerHTML,"(::Title::)",getNode('title'));
  78. output = (document.all) ? Replace(output,"(::Link::)",getNode('link'))
  79. : Replace(output,"%28::Link::%29",getNode('link'));
  80. output = Replace(output,"(::Description::)",getNode('description'));
  81. rssTitle.innerHTML = output;
  82. }
  83. }



  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <rss version="2.0">
  3. <channel>
  4. <title>CNN.com - Technology1</title>
  5. <link>http://www.wp.pl</link>
  6. <description>CNN.com delivers up-to-the-minute news and information on the latest top stories, weather, entertainment, politics and more.</description>
  7. <language>en-us</language>
  8. <copyright>Š 2006 Cable News Network LP, LLLP.</copyright>
  9. <pubDate>Tue, 01 Aug 2006 21:41:01 EDT</pubDate>
  10. <ttl>10</ttl>
  11. <image>
  12. <title>CNN.com - Technology</title>
  13. <link>http://www.cnn.com/rssclick/TECH/?section=cnn_tech</link>
  14. <url>http://i.cnn.net/cnn/.element/img/1.0/logo/cnn.logo.rss.gif</url>
  15. <width>144</width>
  16. <height>33</height>
  17. <description>CNN.com delivers up-to-the-minute news and information on the latest top stories, weather, entertainment, politics and more.</description>
  18. </image>
  19. <item>
  20. <title>Cell phones: A new tool in the war-zone blogosphere </title>
  21. <link>http://www.cnn.com/rssclick/2006/TECH/internet/08/01/newblogs/index.html?section=cnn_tech</link>
  22. <description>Pundits and political junkies may have put blogs on the map. But now individuals all over the planet are using new blogging tools to share gritty, uncensored information.</description>
  23. <pubDate>Tue, 01 Aug 2006 16:06:17 EDT</pubDate>
  24. </item>
  25. <item>
  26. <title>Site offers death alerts for baby boomers</title>
  27. <link>http://www.cnn.com/rssclick/2006/TECH/internet/08/01/babyboomers.reut/index.html?section=cnn_tech</link>
  28. <description>Read full story for latest details.</description>
  29. <pubDate>Tue, 01 Aug 2006 11:06:35 EDT</pubDate>
  30. </item>
  31. <item>
  32. <title>Gaming expo collapses under its own weight</title>
  33. <link>http://www.cnn.com/rssclick/money/2006/08/01/commentary/column_gaming/index.htm?section=cnn_tech</link>
  34. <description>The dueling - and deafening - two-story tall speakers of Electronic Arts and Activision have fought their last battle. E3, the industry's annual trade show, which is known for its overwhelming sights, sounds and excess, is scaling down.</description>
  35. <pubDate>Tue, 01 Aug 2006 18:28:37 EDT</pubDate>
  36. </item>
  37. <item>
  38. <title>Sony at 60: Looking for a jolt of electricity</title>
  39. <link>http://www.cnn.com/rssclick/2006/TECH/biztech/07/31/sonyat60.ap/index.html?section=cnn_tech</link>
  40. <description>Read full story for latest details.</description>
  41. <pubDate>Mon, 31 Jul 2006 11:36:19 EDT</pubDate>
  42. </item>
  43. <item>
  44. <title>Metallica, long resistant, joining iTunes</title>
  45. <link>http://www.cnn.com/rssclick/2006/SHOWBIZ/Music/07/28/metallica.itunes.ap/index.html?section=cnn_tech</link>
  46. <description>Read full story for latest details.</description>
  47. <pubDate>Fri, 28 Jul 2006 17:02:41 EDT</pubDate>
  48. </item>
  49. <item>
  50. <title>Microsoft: Vista shipments on track, for now</title>
  51. <link>http://www.cnn.com/rssclick/2006/TECH/ptech/07/28/microsoft.vista.reut/index.html?section=cnn_tech</link>
  52. <description>Read full story for latest details.</description>
  53. <pubDate>Fri, 28 Jul 2006 11:23:26 EDT</pubDate>
  54. </item>
  55. <item>
  56. <title>Your cell phone is so money</title>
  57. <link>http://www.cnn.com/rssclick/2006/TECH/ptech/07/28/nfc.chip/index.html?section=cnn_tech</link>
  58. <description>Forgot your wallet? You'll need a better excuse than that for passing on the check. By next year, you'll be able to pay simply by swiping your cell phone a few inches from a cash register, with a new wireless standard called Near Field Communication. </description>
  59. <pubDate>Fri, 28 Jul 2006 16:06:04 EDT</pubDate>
  60. </item>
  61. <item>
  62. <title>Review: 'Micro Machines' swaps quality for quantity</title>
  63. <link>http://www.cnn.com/rssclick/2006/TECH/fun.games/07/28/micro.machines/index.html?section=cnn_tech</link>
  64. <description>Racing games come in all shapes and sizes -- from authentic NASCAR and Formula 1 driving simulations to urban street racing challenges to over-the-top arcade handling on fantasy tracks.</description>
  65. <pubDate>Fri, 28 Jul 2006 16:17:47 EDT</pubDate>
  66. </item>
  67. </channel>
  68. </rss>
djgarsi
Jakieś demko moze?smile.gif
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.