<?php
/****
连接数据库,动态生成Rss Feed
****/
class feed{
public $title = '';//channel的title
public $link = '';//channel的link
public $description = '';//channel的description
public $template ='./feed.xml';//xml模板
public $dom = null;
public function __construct()//记得是双下划线
{
$this->dom = new DOMDocument('1.0','utf-8');
$this->dom->load($this->template);
}
//封装createChannel方法,用来创建唯一且必须的channel节点
public function createChannel( )
{
$channel = $this->dom->createElement('channel');
$channel->appendChild($this->createEle('title',$this->title));
$channel->appendChild($this->createEle('link',$this->link));
$channel->appendChild($this->createEle('description',$this->description));
$this->dom->appendChild($channel);
}
//调用createItem,把所有的item节点都生成
public function display()
{
$this->createChannel();
header('content-type:text/xml');
echo $this->dom->savexml();
}
//封装一个方法,用来造item
protected function createItem($arr)
{
$item = $this->dom->createElement('item');
foreach ($$arr as $k => $v) {
$item->appendChild($this->createEle($k,$v));
}
return $item;
}
//封装一个方法,直接创建如<ele>some text</ele>这样的节点
protected function createEle($name,$value)
{
$ele = $this->dom->createElement($name);
$text = $this->dom->createTextNode($value);
$ele->appendChild($text);
return $ele;
}
}
$feed = new feed();
$feed->display();
?>
运行结果如下:
会用到的feed.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<!-- rss输出模板 用php动态制造channel和item -->
<rss version="2.0" xmlns:wfw="http://wellformedweb.org/CommentAPI">
</rss>
2万+

被折叠的 条评论
为什么被折叠?



