文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>如何用PHP把RDF内容插入Web站点之中(3)

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

时间:2007-02-17  来源:PHP爱好者

筑巢时间(Nesting Time)
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO

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

wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
<html>
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
<head>
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
<basefont face="Verdana">
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
</head>
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
<body>
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO

wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
<table border="0" cellspacing="5" cellpadding="5">
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
<tr>
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
<td><b>New releases on freshmeat.net today:</b></td>
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
</tr>
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO

wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
<?php
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
// XML file
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
$file = "http://www.freshmeat.net/backend/fm-releases.rdf";
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO

wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
// set up some variables for use by the parser
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
$currentTag = "";
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
$flag = "";
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
$count = 0;
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO

wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
// this is an associative array of channel data with keys ("title",
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
"link",
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
"description")
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
$channel = array();
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO

wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
// this is an array of arrays, with each array element representing an
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
<item> // each outer array element is itself an associative array
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
// with keys ("title", "link", "description")
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
$items = array();
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO

wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
// opening tag handler
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
function elementBegin($parser, $name, $attributes)
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
{
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
global $currentTag, $flag;
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
$currentTag = $name;
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
// set flag if entering <channel> or <item> block
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
if ($name == "ITEM")
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
{
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
$flag = 1;
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
}
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
else if ($name == "CHANNEL")
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
{
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
$flag = 2;
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
}
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
}
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO

wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
// closing tag handler      
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
function elementEnd($parser, $name)
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
{
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
global $currentTag, $flag, $count;
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
$currentTag = "";
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO

wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
// set flag if exiting <channel> or <item> block
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
if ($name == "ITEM")
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
{
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
$count++;
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
$flag = 0;
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
}
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
else if ($name == "CHANNEL")
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
{
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
$flag = 0;
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
}
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
}
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO

wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
// character data handler
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
function characterData($parser, $data)
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
{
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
global $currentTag, $flag, $items, $count, $channel;
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
$data = trim(htmlspecialchars($data));
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
if ($currentTag == "TITLE" || $currentTag == "LINK" ||
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
$currentTag ==
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
"DEscriptION")
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
{
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
// add data to $channels[] or $items[] array
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
if ($flag == 1)
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
{
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
$items[$count][strtolower($currentTag)] .=
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
$data;
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
}
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
else if ($flag == 2)
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
{
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
$channel[strtolower($currentTag)] .= $data;
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
}
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
}
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO

wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
}
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO

wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
// create parser
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
$xp = xml_parser_create();
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO

wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
// set element handler
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
xml_set_element_handler($xp, "elementBegin", "elementEnd");
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
xml_set_character_data_handler($xp, "characterData");
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
xml_parser_set_option($xp, XML_OPTION_CASE_FOLDING, TRUE);
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
xml_parser_set_option($xp, XML_OPTION_SKIP_WHITE, TRUE);
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO

wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
// read XML file
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
if (!($fp = fopen($file, "r")))
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
{
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
die("Could not read $file");
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
}
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO

wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
// parse data
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
while ($xml = fread($fp, 4096))
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
{
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
if (!xml_parse($xp, $xml, feof($fp)))
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
{
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
die("XML parser error: " .
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
xml_error_string(xml_get_error_code($xp)));
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
}
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
}
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO

wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
// destroy parser
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
xml_parser_free($xp);
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO

wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
// now iterate through $items[] array
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
// and print each item as a table row
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
foreach ($items as $item)
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
{
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
echo "<tr><td><a href=" . $item["link"] . ">" . $item["title"] .
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
"</a><br>" . $item["description"] .  "</td></tr>"; }
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO

wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
?>
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
</table>
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
</body>
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
</html>
wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO

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

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

wwww.chin aitpower.comSUaT7QI0jAY1hcUcyuV9mU1zO
一旦文档分析完毕,事情就简单了——遍历$items 数组,以表格形式打印其中的每一个条目(item)。远行结果如下:
php爱好者站 http://www.phpfans.net c/vc/c++/java.
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载