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

來源:互聯網
上載者:User

 

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],



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.