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],
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器