| 首页 | 新闻 | 网页 | 设计 | 色彩 | 原创 | 视觉 | 素材 | 动漫 | 酷站 | 策划 | 文案 | 访谈 | 运营 | 编程 | 数据库 | 服务器 | 下载 | 图库 | 
您的位置: 幽幽天空 > 网页 > 编程开发 > PHP教程 > 高级应用教程 > 文章正文 用户登录
Blog站点如何用RS
如何优化Blog来提
如何从博客赚钱。
Web 2.0,如何创造
如何发挥Blog的互
企业如何进行博客
如何建立自己的博
如何从博客赚钱
请问如何利用博客
如何推广你的博客

如何用PHP把RDF内容插入Web站点之中(三)           

如何用PHP把RDF内容插入Web站点之中(三)

作者:佚名 来源:CSDN 作者: cyman20(翻译) 更新:2006-8-25 21:05:35 错误报告 我要投稿

筑巢时间(Nesting Time)

前面的例子只是用来说明问题的。如果你真想把RDF内容插入到Web站点当中,就需要把事情做的更好一些。所以把前面的脚本的作了改进,新增了一些东西,从而简化格式化RDF数据的任务。

<html>
<head>
<basefont face="Verdana">
</head>
<body>
 
<table border="0" cellspacing="5" cellpadding="5">
<tr>
<td><b>New releases on freshmeat.net today:</b></td>
</tr>
 
<?php
// XML file
$file = "http://www.freshmeat.net/backend/fm-releases.rdf";
 
// set up some variables for use by the parser
$currentTag = "";
$flag = "";
$count = 0;
 
// this is an associative array of channel data with keys ("title",
"link",
"description")
$channel = array();
 
// this is an array of arrays, with each array element representing an
<item> // each outer array element is itself an associative array
// with keys ("title", "link", "description")
$items = array();
 
// opening tag handler
function elementBegin($parser, $name, $attributes)
{
  global $currentTag, $flag;
  $currentTag = $name;
  // set flag if entering <channel> or <item> block
  if ($name == "ITEM")
  {
           $flag = 1;
  }
  else if ($name == "CHANNEL")
  {
           $flag = 2;
  }
}
 
// closing tag handler      
function elementEnd($parser, $name)
{
  global $currentTag, $flag, $count;
  $currentTag = "";
 
  // set flag if exiting <channel> or <item> block
  if ($name == "ITEM")
  {
           $count++;
           $flag = 0;
  }
  else if ($name == "CHANNEL")
  {
           $flag = 0;
  }
}
 
// character data handler
function characterData($parser, $data)
{
  global $currentTag, $flag, $items, $count, $channel;
  $data = trim(htmlspecialchars($data));
  if ($currentTag == "TITLE" || $currentTag == "LINK" ||
$currentTag ==
"DESCRIPTION")
  {
           // add data to $channels[] or $items[] array
           if ($flag == 1)
           {
                   $items[$count][strtolower($currentTag)] .=
$data;
           }
           else if ($flag == 2)
           {
                   $channel[strtolower($currentTag)] .= $data;
           }
  }
 
}
 
// create parser
$xp = xml_parser_create();
 
// set element handler
xml_set_element_handler($xp, "elementBegin", "elementEnd");
xml_set_character_data_handler($xp, "characterData");
xml_parser_set_option($xp, XML_OPTION_CASE_FOLDING, TRUE);
xml_parser_set_option($xp, XML_OPTION_SKIP_WHITE, TRUE);
 
// read XML file
if (!($fp = fopen($file, "r")))
{
      die("Could not read $file");
}
 
// parse data
while ($xml = fread($fp, 4096))
{
    if (!xml_parse($xp, $xml, feof($fp)))
    {
           die("XML parser error: " .
xml_error_string(xml_get_error_code($xp)));
    }
}
 
// destroy parser
xml_parser_free($xp);
 
// now iterate through $items[] array
// and print each item as a table row
foreach ($items as $item)
{
  echo "<tr><td><a href=" . $item["link"] . ">" . $item["title"] .
"</a><br>" . $item["description"] .  "</td></tr>"; }
 
?>
</table>
</body>
</html>

与先前的那段的主要区别在于,这段脚本创建了两个数组,用于保存分析过程中所提取的信息。其中,$channel是联合性数组(associative array),存放被处理的频道的基本描述信息,而$items是一个二维数组,包含关于单独的频道条目(channel intems)的信息。$items数组中的每一个元素本身又是一个联合性数组,包含title,URL和description关键字。$items数组中元素总数与RDF文档中的<item>区块总数相同。

还需注意$flag变量的变化,根据被处理的是<channel></channel>区块还是<item></item>区块,它现在保存两个值。这一点很有必要,因为只有这样,分析器才能把信息放入正确的数组里面。

一旦文档分析完毕,事情就简单了——遍历$items 数组,以表格形式打印其中的每一个条目(item)。

文章录入:skyuu    责任编辑:skyuu 
  • 上一篇文章:

  • 下一篇文章:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    发表评论:
    姓名:  评 分: 1分 2分 3分 4分 5分
     
  • 严禁发表危害国家安全、政治、黄色淫秽等内容的评论。
  • 用户需对自己在使用幽幽天空服务过程中的行为承担法律责任。
  • 本站管理员有权保留或删除评论内容。
  • 评论内容只代表机友个人观点,与本网站立场无关。