选择显示字体大小

如何用php把rdf内容插入web站点之中(5)

xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /> 

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 (&#36;name == "item")
{
&#36;this->flag = 1,
}
else if (&#36;name == "channel")
{
&#36;this->flag = 2,
}
}

// closing tag handler
function elementend(&#36;parser, &#36;name)
{
&#36;this->currenttag = "",

// set flag if exiting <channel> or <item> block
if (&#36;name == "item")
{
&#36;this->count++,
&#36;this->flag = 0,
}
else if (&#36;name == "channel")
{
&#36;this->flag = 0,
}
}

// character data handler
function characterdata(&#36;parser, &#36;data)
{
&#36;data = trim(htmlspecialchars(&#36;data)),
if (&#36;this->currenttag == "title" &#36;this->currenttag ==
"link" &#36;this->currenttag == "description")
{
// add data to &#36;channels[] or &#36;items[] array
if (&#36;this->flag == 1)
{

&#36;this->items[&#36;this->count][strtolower(&#36;this->currenttag)] .= &#36;data,
}
else if (&#36;this->flag == 2)
{

&#36;this->channel[strtolower(&#36;this->currenttag)] .= &#36;data,
}
}
}

// return an associative array containing channel information
// (the &#36;channel[] array)
function getchannelinfo()
{
return &#36;this->channel,
}

// return an associative array of arrays containing item
information
// (the &#36;items[] array)
function getitems()
{
return &#36;this->items,
}

}
?>
如果你对php类较为熟悉的话,那么理解这段代码是相当容易的。如果不太懂的话,那么请直接跳到文章末尾的链接部分,看一篇关于类工作原理的好文章。然后在回来继续阅读上面的代码。
在使用这个类之前,我要特别花几分钟指出其中的一行代码——即上面对xml_set_object()函数调用的那一行。
现在的问题是如何使用这个类实际生成具有多个内容来源的web页。
<?
include("class.rdfparser.php"),
// how many items to display in each channel
&#36;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
&#36;f = new rdfparser(),
&#36;f->setresource("http://www.freshmeat.net/backend/fm-releases.rdf"),
&#36;f->parseresource(),
&#36;f_channel = &#36;f->getchannelinfo(),
&#36;f_items = &#36;f->getitems(),
// now format and print it...
?>
the latest from <a href=<? echo &#36;f_channel["link"], ?>><? echo
&#36;f_channel["title"], ?></a> <br> <ul> <? // iterate through items array
for (&#36;x=0, &#36;x<&#36;maxitems, &#36;x++) {
if (is_array(&#36;f_items[&#36;x]))
{
// print data
&#36;item = &#36;f_items[&#36;x],
echo "<li><a href=" . &#36;item["link"] . ">" .
&#36;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
&#36;s = new rdfparser(),
&#36;s->setresource("http://slashdot.org/slashdot.rdf"),
&#36;s->parseresource(),
&#36;s_channel = &#36;s->getchannelinfo(),
&#36;s_items = &#36;s->getitems(),
// now format and print it...
?>
the latest from <a href=<? echo &#36;s_channel["link"], ?>><? echo
&#36;s_channel["title"], ?></a> <br> <ul> <? // iterate through items array
for (&#36;x=0, &#36;x<&#36;maxitems, &#36;x++) {
if (is_array(&#36;s_items[&#36;x]))
{
// print data
&#36;item = &#36;s_items[&#36;x],
echo "<li><a href=" . &#36;item["link"] . ">" .
&#36;item["title"] . "</a>",
}
}
?>
</ul>
</font>
</td>

</tr>
</table>

</body>
</head>
</html>

这段代码相当简单。一旦你用“new”关键字生成一个类的实例,
&#36;f = new rdfparser(),
那么就可以用类方法来设置要分析的rdf文件的位置,
&#36;f->setresource("http://www.freshmeat.net/backend/fm-releases.rdf"),
&#36;f->parseresource(),
并且获取&#36;channel&#36;items数组,以用于后面的处理。

<?
&#36;f_channel = &#36;f->getchannelinfo(),
&#36;f_items = &#36;f->getitems(),
?>

the latest from <a href=<? echo &#36;f_channel["link"], ?>><? echo
&#36;f_channel["title"], ?></a> <br> <ul> <? // iterate through items array
for (&#36;x=0, &#36;x<&#36;maxitems, &#36;x++) {
if (is_array(&#36;f_items[&#36;x]))
{
// print data
&#36;item = &#36;f_items[&#36;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   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons