注册 | 登录
收藏 | 帮助
热门文章
编辑推荐
相关文章  
如何根据名称识别计算机病毒
教你如何防止脚本病毒执行的通用
在网吧如何防止病毒和快速杀毒
如何判断电脑是否感染了病毒
特络伊木马如何利用文件关联和设
深入揭秘木马是如何盗取QQ密码的
如何知道你的电脑是否含有病毒
杀毒软件如何被XP SP2的安全中心
教你如何手工剿灭QQ广告弹出的木
木马是如何编写的(一)
您现在的位置: 顶尖设计 >> IT学院 >> 编程开发 >> PHP >> 文章正文
如何用PHP把RDF内容插入Web站点之中(四)
作者:cyman20(翻译)  来源:CSDN  点击:  更新:2006-12-19
简介:

返回到类(Back To Class)

既然你有这么大的权力,那么究竟为什么要把自己限制在仅仅是单个的RDF来源呢?就象我早先说过的一样,大多数主要的站点都经常为他们所提供的内容做快照。其实将所有这些不同的来源插入到你的站点当中是相当简单的。让我们看看是如何做的。

首先,我们把前面例子中的代码模块化。这样一来,你就无须为每一个单个的来源都一遍又一遍的重写相同的代码了。简化的方法就是将之打包成类,再把这个类包含到我的PHP脚本当中。

类代码如下:

<?
class RDFParser
{
  //
  // variables
  //
 
  // set up local variables for this class
  var $currentTag = "";
  var $flag = "";
  var $count = 0;
 
  // this is an associative array of channel data with keys
("title", "link", "description")
  var $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")
  var $items = array();
 
 
  //
  // methods
  //
 
  // set the name of the RDF file to parse
  // this is usually a local file
  // you may set it to a remote file if your PHP build supports
URL fopen()
  function setResource($file)
  {
           $this->file = $file;
  }
 
 
  // parse the RDF file set with setResource()
  // this populates the $channel and $items arrays
  function parseResource()
  {
           // create parser
           $this->xp = xml_parser_create();
          
           // set object reference
           xml_set_object($this->xp, $this);
          
           // set handlers and parser options
           xml_set_element_handler($this->xp, "elementBegin",
"elementEnd");
           xml_set_character_data_handler($this->xp,
"characterData");
           xml_parser_set_option($this->xp,
XML_OPTION_CASE_FOLDING, TRUE);
           xml_parser_set_option($this->xp, XML_OPTION_SKIP_WHITE,
TRUE);
          
           // read XML file
           if (!($fp = fopen($this->file, "r")))
           {
                 die("Could not read $this->file");
           }
          
           // parse data
           while ($xml = fread($fp, 4096))
           {
               if (!xml_parse($this->xp, $xml, feof($fp)))
               {
                            die("XML parser error: " .
xml_error_string(xml_get_error_code($this->xp)));
               }
           }
          
           // destroy parser
           xml_parser_free($this->xp);
  }
 
  // opening tag handler
  function elementBegin($parser, $name, $attributes)
  {
           $this->currentTag = $name;
           // set flag if entering <channel> or <item> block
           if ($name == "ITEM")
           {
                   $this->flag = 1;
           }
           else if ($name == "CHANNEL")
           {
                   $this->flag = 2;
           }
  }
 
  // closing tag handler      
  function elementEnd($parser, $name)
  {
           $this->currentTag = "";
 
           // set flag if exiting <channel> or <item> block
           if ($name == "ITEM")
           {
                   $this->count++;
                   $this->flag = 0;
           }
           else if ($name == "CHANNEL")
           {
                   $this->flag = 0;
           }
  }
 
  // character data handler
  function characterData($parser, $data)
  {
           $data = trim(htmlspecialchars($data));
           if ($this->currentTag == "TITLE" || $this->currentTag ==
"LINK" || $this->currentTag == "DESCRIPTION")
           {
                   // add data to $channels[] or $items[] array
                   if ($this->flag == 1)
                   {
 
$this->items[$this->count][strtolower($this->currentTag)] .= $data;
                   }
                   else if ($this->flag == 2)
                   {
 
$this->channel[strtolower($this->currentTag)] .= $data;
                   }
           }
  }       
 
  // return an associative array containing channel information
  // (the $channel[] array)
  function getChannelInfo()
  {
           return $this->channel;
  }
 
  // return an associative array of arrays containing item
information
  // (the $items[] array)
  function getItems()
  {
           return $this->items;
  }
 
}
?>

如果你对PHP类较为熟悉的话,那么理解这段代码是相当容易的。如果不太懂的话,那么请直接跳到文章末尾的链接部分,看一篇关于类工作原理的好文章。然后在回来继续阅读上面的代码。

在使用这个类之前,我要特别花几分钟指出其中的一行代码——即上面对xml_set_object()函数调用的那一行。

现在的问题是如何使用这个类实际生成具有多个内容来源的Web页。

<?
include("class.RDFParser.php");
// how many items to display in each channel
$maxItems = 5;
?>
<html>
<head>
<basefont face="Verdana">
<body>
 
<table width="100%" border="0" cellspacing="5" cellpadding="5"> <tr>
<!-- first cell -->
<td valign=top align=left>
<font size="-1">
<?
// get and parse freshmeat.net channel
$f = new RDFParser();
$f->setResource("http://www.freshmeat.net/backend/fm-releases.rdf");
$f->parseResource();
$f_channel = $f->getChannelInfo();
$f_items = $f->getItems();
// now format and print it...
?>
The latest from <a href=<? echo $f_channel["link"]; ?>><? echo
$f_channel["title"]; ?></a> <br> <ul> <? // iterate through items array
for ($x=0; $x<$maxItems; $x++) {
  if (is_array($f_items[$x]))

[1] [2] 下一页






  • 上一篇文章:
  • 下一篇文章:
  • 分享此文:该页面添加到 Mister Wong 添加到雅虎Yahoo!收藏 Add to:Del.icio.us Post to Furl Digg this 添加到Google书签 reddit spurl blogmarks 365Key 评论  收藏  分享  打印
     我来说两句
    姓名:       验证码:   
    主页: 
    评分: 1分 2分 3分 4分 5分
    本频道近期热评文章:
      关于我们 | 联系我们 | 站点地图 | 广告投放 | 友情链接 | 在线留言 | 版权申明
    版权所有 © 2004-2007 顶尖设计(bobd.cn)
    未经授权禁止转载,摘编,复制本站内容或建立镜像. 沪ICP备07504942号 
    网络110
    报警服务