如何用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
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
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,
}
}
?>
在使用这个类之前,我要特别花几分钟指出其中的一行代码——即上面对xml_set_object()函数调用的那一行。
现在的问题是如何使用这个类实际生成具有多个内容来源的Web页。
include("class.RDFParser.php"),
// how many items to display in each channel
$maxItems = 5,
?>
basefont face="Verdana">
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],