通过使用相同的 RSS 解析器, 可以将下列联机内容提供给 Flash Lite 1.1 RSS 阅读器:
通过使用 RSS 1.0、RSS 2.0、RDF 或 Atom 规范, 所有这些 Web 内容类型当前都可用。
对于此示例, 我将使用 PHP 5 编程语言在服务器上开发 RSS 解析器。 您可以使用您喜爱的编程语言 (如 ColdFusion、JSP、ASP 或 Perl)来达到相同的结果。 服务器脚本是可开始构建更加复杂的 RSS 解析器的基本框架。 对于此示例, 您将解析来自下列 Web 地址的 RSS 2.0 服务: http://www.flashmobilegroup.org/?feed=rss2
此处介绍 RSS 2.0 XML 服务部分的样子:
<rss version="2.0">
−<channel>
<title>Flashmobilegroup.org</title>
<link>http://www.flashmobilegroup.org</link>
<description/>
<pubDate>Tue, 06 Jun 2006 12:51:45 +0000</pubDate>
<generator>http://wordpress.org/?v=1.5.2</generator>
<language>en</language>
<item>
<title>Tips for Developing Flash Games for the iRiver U10</title>
<description>New article on DevNet at Adobe: Tips for Developing Flash Games for the iRiver U10. It includes two samples available for download.
The article is written by Sung-Hee Park of MiniGate. Alessandro
</description>
</item>
............
............
............
<channel>
您将解析此 RSS 2.0 服务来为每个 <item> 块提取 <title> 和 <description> 标签, 然后自定义能够加载到手机上的 Flash Lite 1.1 RSS 阅读器中的内容。
因为 PHP 5 有非常简单的 XML 解析 API, 所以我选择它作为服务器端脚本语言。 此处是脚本:
//Link to RSS 2.0 feed
$link_to_rss_feed ="http://www.flashmobilegroup.org/?feed=rss2";
//Counter to append to &variables to return to the client
$i=1;
// This PHP 5 function loads the entire XML feed
$xml = simplexml_load_file($link_to_rss_feed);
// Parse each item in the RSS feed
foreach ($xml->channel[0]->item as $item)
{
if($item) {
// Need to strip html tags since Flash Lite does not support
// html in dynamic text fields
$title = strip_tags($item->title);
$description = strip_tags($item->description);
// Replace special symbols &
$title = str_replace("&", "", $title);
$description = str_replace("&", "", $description);
// Return the content with the appropriate index
echo "&title$i=".$title;
echo "&description$i=".$description;
//echo $i;
$i++;
} else {
// Send and Error message if content is not found
echo "&msg=Content not found";
echo "&flag=error";
echo "&end=end";
exit;
}
}
$i=$i-1;
echo "&flag=ok";
echo "&totalitems=".$i;
echo "&end=end";
定义到 RSS 2.0 服务的链接:
$link_to_rss_feed ="http://www.flashmobilegroup.org/?feed=rss2";
如下所述, 上面的链接将成为 simplexml_load_file PHP 5 功能调用的输入:
$xml = simplexml_load_file($link_to_rss_feed);
$xml 变量现在包含来自上述链接的整个 XML 服务。 注意, 上述功能可以直接从 Internet 访问 XML 内容, 但它受 PHP 服务器引擎的安全限制的影响 (有关详细信息, 请访问 www.php.net*)。
此时, 您将感兴趣于解析每个 <item> 节点并选择要返回到 Flash Lite 1.1 应用程序中的 <title> 和 <description> 的内容。 出于此原因, 您使用 foreach 功能来循环每个节点并提取内容。
首先, 您需要从包含在 <title> 和 <description> 标签中的文本取掉 HTML 标签。 这可以通过使用 strip_tags 功能来完成。 Flash Lite 1.1 动态文本不支持 HTML 标签。
另外, 非常重要的是, 您必须取掉在 <title> 和 <description> 标签之间的文本中包含的任何 & 符号。 这可以通过使用 str_replace 功能来完成。 Flash Lite 1.1 通过将 & 符号作为前缀, 认识到变量数据已加载, 因此包含在文本中的任何 & 将打破此数据结构。
下面两行将为 title 和 description 创建 & 名称/值对数据结构, 它们将成为在 Flash Lite 1.1 RSS 阅读器中使用的变量:
echo "&title$i=".$title; echo "&description$i=".$description;
上面的回显将为第一个 <item> 块返回下列输出:
&title1=为 iRiver U10 开发 Flash 游戏的技巧&description1=Adobe 的 DevNet 上的新文章: 为 iRiver U10 开发 Flash 游戏的技巧。 它包含两个可供下载的示例。 本文是 MiniGate 的 Sung-Hee Park 撰写的。 Alessandro
因为 RSS 服务包含多个项目节点, 所以您将结束拥有几个 &title 和 &description 数据的情况, 比如 &item1、 &description1、 &item2、 &description2等等。 将循环索引 $i 追加到每个项目和描述变量以创建每个节点的不同集。
&totalitems 是已解析的项目的总数, 而 &end 变量是我们将用于指示到数据所在的 Flash Lite 1.1 的标记。
Flash Lite 1.1 没有检查数据结尾的功能。 因此, 此问题的解决办法是在数据的结尾处返回一个标记。
注意: 您可以使用其他方法达到相同的结果。 同时, 请记住您很可能将需要更多的错误检查。
此时, 您已构建了服务器 RSS 解析器的主要结构。 请将该文件置于可从 Internet 访问的服务器上。 要测试解析器, 只需将到该脚本的链接输入到浏览器中。