作者: luckiejacky 時間: 2017-2-12 18:46 標題: RSS 讀唔到內文
![CDATA] tag 內的description點可以讀到?
THX
- <channel>
- <title>九天天氣預報</title>
- <link>http://www.weather.gov.hk/wxinfo/currwx/fndc.htm</link>
- <description>九天天氣預報</description>
- <language>zh-tw</language>
- <webMaster>mailbox@hko.gov.hk</webMaster>
- <copyright>本檔案的內容,包括但不限於所有文本、平面圖像、圖畫、圖片、照片以及數據或其他資料的匯編,均受版權保障。香港特別行政區政府是本檔案內所有版權作品的擁有人,除非預先得到香港天文台的書面授權,否則嚴禁複製、改編、分發、發布或向公眾提供該等版權作品。
- </copyright>
- <image>
- <url>http://rss.weather.gov.hk/img/logo_dblue.gif</url>
- <title>九天天氣預報</title>
- <link>http://www.weather.gov.hk/</link>
- </image>
- <item>
- <guid isPermaLink="false">http://rss.weather.gov.hk/rss/SeveralDaysWeatherWeatherForecast/20170212163000</guid>
- <pubDate>Sun, 12 Feb 2017 08:30:00 GMT</pubDate>
- <title>香港天文台於2017年02月12日16時30分發出之天氣報告</title>
- <category>F</category>
- <author>香港天文台</author>
- <link>http://www.weather.gov.hk/wxinfo/currwx/fndc.htm</link>
- <description><![CDATA[
-
- 天 氣 概 況 :<br/>一 股 偏 東 氣 流 會 在 未 來 數 天 影 響 華 南 沿 岸 地 區 , 該 區 氣 溫 將 逐 漸 回 升 。 預 料 受 海 洋 氣 流 影 響 , 接 近 週 末 廣 東 沿 岸 較 為 潮 濕 及 有 薄 霧 。 <p/><p/> 二 月 十 三 日 ( 星 期 一 ) <br/>
- 風 :
- 東 至 東 北 風 4 至 5 級 , 晚 上 離 岸 間 中 6 級 。 <br/>
- 天 氣 :
- <!DOCTYPE html>
- <html>
- <head>
- <title>jquery.rss zotero example</title>
- <link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css">
- <script src="http://code.jquery.com/jquery-1.8.3.js"></script>
- <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
- <script src="../src/jquery.rss.js"></script>
- <script>
- var readTokenFromResponse = function(response, attr) {
- return $(response).find('tr th:contains(' + attr + ')').parent().find('td').text()
- }
- jQuery(function($) {
- $("#rss-feeds").rss("http://rss.weather.gov.hk/rss/SeveralDaysWeatherForecast_uc.xml", {
- entryTemplate: "<ul><li>Title: {title}</li> <li>Author: {author}</li> <li>Desc: {description}</li></ul><br>",
- tokens: {
- year: function(entry, tokens) {
- return new Date(entry.publishedDate).getFullYear()
- }
- }
- })
- })
- </script>
- </head>
- <body>
- <div>
- <h1>jquery.rss zotero example</h1>
- <div id="rss-feeds"></div>
- </div>
- </body>
- </html>
作者: luckiejacky 時間: 2017-2-14 12:40
本帖最後由 luckiejacky 於 2017-2-14 13:54 編輯
點解硬系讀唔到description?
- jQuery(function($)
- {
- $("#rss-feeds").rss("http://rss.weather.gov.hk/rss/SeveralDaysWeatherForecast_uc.xml",
- {
- entryTemplate:
- "title: {title}"
-
- }).show();
- })
- var rssurl = "http://rss.weather.gov.hk/rss/SeveralDaysWeatherForecast_uc.xml";
-
- document.write(rssurl);
-
- $.get(rssurl, function(data)
- {
- var $xml = $(data);
- $xml.find("item").each(function()
- {
- var $this = $(this),
- item =
- {
- title: $this.find("title").text(),
- //link: $this.find("link").text(),
- //description: $this.find("description").text(),
- //pubDate: $this.find("pubDate").text(),
- //author: $this.find("author").text()
- }
- //Do something with item here...
- document.write(item.title);
- });
- });
[attach]1960110[/attach]
作者: marlin12 時間: 2017-2-14 23:09
你讀唔到內文,因為個XML轉做JSON果時,畀jQuery.rss.js改晒啲tag name。
如果要攞XML既<description>,係javascript就要用{body}。
用jQuery.rss.js呢件野好危險,佢要用一個www.feedrapp.info既web server,黎將XML轉做JSON。好處係唔使用XMLHttpRequest(無security既問題),壞處當然係要依賴佢個web server,如果佢down機或者做錯野,你就無晒辦法。
我建議你都係用番啲stand-alone既plug-in (例如:jFeed 之類)。
- <!DOCTYPE html>
- <html>
- <head>
- <title>jquery.rss zotero example</title>
- <link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css">
- <script src="http://code.jquery.com/jquery-1.8.3.js"></script>
- <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
- <script src="./jquery.rss.js"></script>
- <script>
- jQuery( function($) {
- $("#rss-feeds").rss("http://rss.weather.gov.hk/rss/SeveralDaysWeatherForecast_uc.xml", {
- entryTemplate: "<ul><li>Title = {title}</li><li>{body}</li></ul>",
- tokens: {
- body: function(entry, tokens) {
- return "Content =" + entry.content;
- }
- }
- })
- })
- </script>
- </head>
- <body>
- <div id="rss-feeds"></div>
- </body>
- </html>
作者: MacMonster 時間: 2017-2-14 23:24
如果係PHP就易搞:
- $obj = simplexml_load_string($xmlString, 'SimpleXMLElement', LIBXML_NOCDATA);
作者: luckiejacky 時間: 2017-2-15 11:42
Thanks ching..
唔該晒...
作者: luckiejacky 時間: 2017-2-15 12:07
想問多一問,如果用ajax parse,
如果個xml是static的,這是okay
但from url,就唔work.... Why?
- $.ajax({
- //url: 'xml/weather.xml',
- url: "http://rss.weather.gov.hk/rss/SeveralDaysWeatherForecast_uc.xml",
- dataType: 'xml',
- success: function(data)
- {
- $(data).find('channel item').each(function() {
- var title = $(this).find('title').text();
- document.write(title);
-
- var description = $(this).find('description').text();
- document.write(description);
- });
-
- },
- error: function() {
- $('#weather').text('failed to read feed');
- }
-
- });
作者: marlin12 時間: 2017-2-16 00:53
想問多一問,如果用ajax parse,
如果個xml是static的,這是okay
但from url,就唔work.... Why? ...
問題係你個AJAX直接去讀取另一個domain既野,違返咗Same-origin policy,個browser咪唔畀你咁做。
至於讀xml/weather.xml果個file,因為係同一個server入面,咪無問題囉。
同源政策 (Same-origin policy)
作者: luckiejacky 時間: 2017-2-16 10:53
唔該多一次
Thank you
作者: carlkyo 時間: 2018-8-14 09:38
http://api.wunderground.com/auto ... ndex.xml?query=RCSS
香港天文台既XML做得好差~~
