zoukankan      html  css  js  c++  java
  • xml 制作 RSS 订阅源

    首先制作一个 RSS 模板,模板的文件名是 feed.xml,代码如下:

    <?xml version="1.0" encoding="utf-8"?>
    <rss version="2.0" xmlns:wfw="http://wellformedweb.org/CommentAPI/"></rss>

    再就是用php文件从数据库读取数据并生成 RSS 文件,这里用一个数组模拟从数据库读取的数据:

    <?php 
    class Rss{
        protected $dom = null;
        protected $temp = './feed.xml';
        protected $rss = null;
        protected $title = '';
        protected $desc = '';
        protected $link = '';
        
        public function __construct(){
            $this->title = '物理学';
            $this->desc = '现代物理学';
            $this->link = 'http://mysql/rss.php';
            $this->dom = new DOMDocument('1.0','utf-8');
            $this->dom->load($this->temp);
            $this->rss = $this->dom->getElementsByTagName('rss')->item(0);
        }
        
        public function feed($arr){
            $this->createChannel();
            $channel = $this->dom->getElementsByTagName('channel')->item(0);
            foreach ($arr as $v){
                $channel->appendChild($this->createItem($v));
            }
            header('content-type:text/xml');
            echo $this->dom->savexml();
        }
        
        protected 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->desc));
            $this->rss->appendChild($channel);
        }
    
        protected function createItem($arr){
            $item = $this->dom->createElement('item');
            foreach($arr as $k => $v){
                $item->appendChild($this->createEle($k,$v));
            }
            return $item;
        }
        
        protected function createEle($name,$value){
            $e=$this->dom->createElement($name);
            $t=$this->dom->createTextNode($value);
            $e->appendChild($t);
            return $e;
        }
    }
    $arr = array( array( 'title'=>'牛顿力学', 'link'=>'1', 'description'=>'牛顿力学' ), array( 'title'=>'相对论', 'link'=>'1', 'description'=>'爱因斯坦的相对论' ) ); $rss = new Rss; $rss->feed($arr); ?>

    最后在火狐下效果:

  • 相关阅读:
    javascript获取客户端网卡MAC地址和IP地址和计算机名
    Spring 架包功能详解
    MyEclipse安装插件的另一种方法
    (转载)Struts Spring Hibernate (SSH) 整合实例
    jsp 输出流写中文出现乱码的解决办法
    通过Ssh协议连接到服务器执行执行的指令
    贝叶斯过滤算法
    IntelliJ IDEA 9.0.3配置tomcat5
    (spring+hibernate)java.lang.NoSuchMethodError
    struts1.2总结
  • 原文地址:https://www.cnblogs.com/programs/p/5509291.html
Copyright © 2011-2022 走看看