搜尋
首頁php教程php手册如何用PHP把RDF内容插入Web站点之中(5)

 

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 or 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 or 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,
?>


basefont
face="Verdana">



cellspacing="5" cellpadding="5"> tr>









tr>
valign=top align=left>


// 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 href= echo $f_channel["link"], ?>> echo
$f_channel["title"], ?>
br> ul> // iterate through items array
for ($x=0, $xmaxItems, $x++) {
if (is_array($f_items[$x]))
{
// print data
$item = $f_items[$x],
echo "li>href=" . $item["link"] . ">" .
$item["title"] . "
",
}
}
?>
ul>


i>Primary page content herei>
valign=top align=left>


// 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 href= echo $s_channel["link"], ?>> echo
$s_channel["title"], ?>
br> ul> // iterate through items array
for ($x=0, $xmaxItems, $x++) {
if (is_array($s_items[$x]))
{
// print data
$item = $s_items[$x],
echo "li>href=" . $item["link"] . ">" .
$item["title"] . "
",
}
}
?>
ul>







这段代码相当简单。一旦你用“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 href
= echo $f_channel["link"], ?>> echo
$f_channel["title"], ?> br> ul> // iterate through items array
for ($x=0, $xmaxItems, $x++) {
if (is_array($f_items[$x]))
{
// print data
$item = $f_items[$x],



陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。