国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > PHP > 正文

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

2019-09-08 23:11:37
字體:
來源:轉載
供稿:網友

筑巢時間(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")
 {
/t   $flag = 1;
 }
 else if ($name == "CHANNEL")
 {
/t   $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")
 {
/t   $count++;
/t   $flag = 0;
 }
 else if ($name == "CHANNEL")
 {
/t   $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")
 {
/t   // add data to $channels[] or $items[] array
/t   if ($flag == 1)
/t   {
/t/t   $items[$count][strtolower($currentTag)] .=
$data;
/t   }
/t   else if ($flag == 2)
/t   {
/t/t   $channel[strtolower($currentTag)] .= $data;
/t   }
 }

}

// 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)))
   {
/t   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)。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 苏尼特右旗| 崇文区| 仁怀市| 天祝| 剑河县| 武冈市| 闽清县| 喀什市| 林口县| 晴隆县| 阳泉市| 黄梅县| 珠海市| 韶关市| 句容市| 金湖县| 诸暨市| 德惠市| 宜川县| 贵阳市| 岱山县| 庆阳市| 阜阳市| 彰化市| 广昌县| 皋兰县| 磴口县| 延长县| 渝北区| 吴桥县| 金湖县| 秦皇岛市| 鄢陵县| 四川省| 繁昌县| 金沙县| 昭觉县| 舒兰市| 高淳县| 扶风县| 纳雍县|