动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
   PHP → 阅读文章

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

作者来源: 
阅读 1152 人次 , 2006-4-18 15:36:00 

 

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]))
{
// print data
$item = $f_items[$x],
echo "<li><a href=" . $item["link"] . ">" .
$item["title"] . "</a>",
}
}
?>
</ul>
</font>
</td>

<!-- second cell -->
<td align=center width=50%>
<i>Primary page content here</i>
</td>

<!-- third cell -->
<td valign=top align=left>
<font size="-1">
<?
// get and parse slashdot.org channel
$s = new RDFParser(),
$s->setResource("http://slashdot.org/slashdot.rdf"),
$s->parseResource(),
$s_channel = $s->getChannelInfo(),
$s_items = $s->getItems(),
// now format and print it...
?>
The latest from <a href=<? echo $s_channel["link"], ?>><? echo
$s_channel["title"], ?></a> <br> <ul> <? // iterate through items array
for ($x=0, $x<$maxItems, $x++) {
if (is_array($s_items[$x]))
{
// print data
$item = $s_items[$x],
echo "<li><a href=" . $item["link"] . ">" .
$item["title"] . "</a>",
}
}
?>
</ul>
</font>
</td>

</tr>
</table>

</body>
</head>
</html>

这段代码相当简单。一旦你用“new”关键字生成一个类的实例,
$f = new RDFParser(),
那么就可以用类方法来设置要分析的RDF文件的位置,
$f->setResource("http://www.freshmeat.net/backend/fm-releases.rdf"),
$f->parseResource(),
并且获取$channel$items数组,以用于后面的处理。

<?
$f_channel = $f->getChannelInfo(),
$f_items = $f->getItems(),
?>

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]))
{
// print data
$item = $f_items[$x],

  
 
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:如何用PHP把RDF内容插入Web站点之中(6)
· 下一篇:如何用PHP把RDF内容插入Web站点之中(4)
· 如何利用php+mysql保存和输出文件
· 使用PHP4中的 IntegratedTemplate类实现HTML和PHP代码分离
· 关于SESSION的补充
· PHP新手上路(五)
· PHP中显示格式化的用户输入


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077