六月婷婷综合激情-六月婷婷综合-六月婷婷在线观看-六月婷婷在线-亚洲黄色在线网站-亚洲黄色在线观看网站

明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

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

[摘要]筑巢時間(Nesting Time)前面的例子只是用來說明問題的。如果你真想把RDF內容插入到Web站點當中,就需要把事情做的更好一些。所以把前面的腳本的作了改進,新增了一些東西,從而簡化格式化RDF數據的任務。<html><head><basefont face=&...
筑巢時間(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>"; }

?>

</body>
</html>
與先前的那段的主要區別在于,這段腳本創建了兩個數組,用于保存分析過程中所提取的信息。其中,$channel是聯合性數組(associative array),存放被處理的頻道的基本描述信息,而$items是一個二維數組,包含關于單獨的頻道條目(channel intems)的信息。$items數組中的每一個元素本身又是一個聯合性數組,包含title,URL和description關鍵字。$items數組中元素總數與RDF文檔中的<item>區塊總數相同。

還需注意$flag變量的變化,根據被處理的是<channel></channel>區塊還是<item></item>區塊,它現在保存兩個值。這一點很有必要,因為只有這樣,分析器才能把信息放入正確的數組里面。

一旦文檔分析完畢,事情就簡單了——遍歷$items 數組,以表格形式打印其中的每一個條目(item)。



主站蜘蛛池模板: 天天躁夜夜躁狠狠躁2024 | 欧洲成品大片在线播放 | 色呦呦在线观看视频 | 日日夜夜摸 | 亚洲第一视频在线 | 依人在线| 亚洲精品综合网 | 日本高清免费不卡在线播放 | 日韩在线视 | 亚洲精品国产精品乱码不卞 | 日韩私人影院 | 日本草草影院 | 午夜久 | 欧美又肥又胖的大bbwⅹ | 亚洲va欧美va天堂v国产综合 | 四虎精品免费永久在线 | 青青草原成人 | 在线免费观看亚洲 | 日韩大片免费观看视频播放 | 亚洲mv在线观看 | 色综合综合在线 | 亚洲成人免费看 | 日韩欧美中国a v | 欧美视频第一页 | 亚洲最色网| 亚洲伦乱 | 亚洲欧美综合一区二区三区四区 | 五月天婷婷网址 | 日日夜夜婷婷 | 亚洲国产日韩欧美在线 | 日本精品高清一区二区2021 | 亚洲国产高清在线精品一区 | 伊人网网站| 亚洲视频在线观看免费视频 | 日韩精品一区二区在线观看 | 日产日韩亚洲欧美综合搜索 | 亚洲欧美成人在线 | 亚色一区 | 四虎免费永久在线播放 | 亚欧精品一区二区三区四区 | 日韩精品视频免费观看 |