大部分是从老师那里学来的,只不过可能有细小的差别
<?php
class rss{
public $title='rss订阅器';
public $description='首次执行商城订阅器';
public $link='http://localhost/shop';
public $data=array();
public $dom=null;
public $root=null;
public function __construct(){
$this->dom=new DOMDocument('1.0','utf-8');
$this->dom->load('rss.xml');
$this->root=$this->dom->getElementsByTagName('rss')->item(0);
}
//create item
public function createEle($elementName,$textValue){
$ele=$this->dom->createElement($elementName);
$text=$this->dom->createTextNode($textValue);
$ele->appendChild($text);
return $ele;
}
//create channel
public function createChannel(){
$channel=$this->dom->createElement('channel');
$channel->appendChild($this->createEle('title', $this->title));
$channel->appendChild($this->createEle('description', $this->description));
$channel->appendChild($this->createEle('link',$this->link));
$this->root->appendChild($channel);
}
public function addItem(){
foreach($this->data as $v){
$item=$this->dom->createElement('item');
foreach($v as $key=>$value){
$item->appendChild($this->createEle($key, $value));
}
$this->root->appendChild($item);
}
}
public function show(){
$this->createChannel();
$this->addItem();
header("content-type:text/xml");
echo $this->dom->saveXML();
}
}
$conn=mysql_connect("localhost",'root','');
mysql_query('set names utf8',$conn);
mysql_query('use shop');
$sql="select goods_name as title, shop_price as description from ecs_goods";
$res=mysql_query($sql,$conn);
$list=array();
while($row=(mysql_fetch_assoc($res))){
$list[]=$row;
}
$res=new rss();
$res->data=$list;
$res->show();
?>
rss.xml代码如下
<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:wfw="http://wellformedweb.org/CommentAPI/"></rss>