rss是一种网页内容联合格式,是xml的一种,所有的rss文档都遵循xml1.0规范。
具体示例:
<span style="font-family:microsoft yahei;font-size:18px;"><?xml version = "1.0" encoding = "utf-8" ?>
<rss version = "2.0" xmlns:wfw ="http://wellformedweb.org/commentapi/">
<channel>
<title>标题</title>
<link>链接地址</link>
<description>描述</description>
<language>描述语言</language>
<copyright>版本</copyright>
<pubdate>时间</pubdate>
<item>
<title>日志标题</title>
<link>日志的url访问地址</link>
<author>日志的作者</author>
<pubdate>日志的发布时间</pubdate>
<description>日志的内容</description>
</item>
</channel>
</rss></span>
此时应该链接数据库,输出想要的结果。
test.php
<span style="font-family:microsoft yahei;font-size:18px;"><?php
include("./conn.php");
class test{
public $title = '';
public $link = '';
public $description= '';
public $items = '';
public $template ='./test.xml';
public $dom = '';
public $rss = '';
public function__construct(){
$this ->dom = new domdocument('1.0','utf-8');
$this ->dom -> load($this -> template);
$this ->rss = $this -> dom -> getelementsbytagname('rss');
}
public functioncreatechannel(){
$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 ->rss -> appendchild($channel);
}
public functioncreateele($name,$value){
$element =$this -> dom -> createelement($name);
$text = $this-> dom -> createtextnode($value);
$element-> appendchild($text);
return$element;
}
protected functionadditem($list){
foreach($listas $goods){
$this-> rss -> appendchild($this-> createitem($goods));
}
}
public functioncreateitem($arr){
$item = $this-> dom -> createelement("item");
foreach($arras $key => $value){
$item-> appendchild($this -> createele($key,$value));
}
return $item;
}
public functiondisplay(){
$this ->createchannel();
$this ->additem($this -> items);
echo $this-> dom -> savexml();
}
}
$sql = "select * fromstu";
$rs = mysql_query($sql);
while($row =mysql_fetch_assoc($rs)){
$list[] = $row;
}
$test = new test();
$test -> title = "测试标题";
$test -> link = "测试连接";
$test -> description ="测试内容";
$test -> display();
$test -> items = $list;
</span>
以上就是利用xml实现rss订阅的详细内容。