#native_company# #native_desc#
#native_cta#

A Quick PHP XMLWriter Class Tutorial: XML & RSS Page 2

By PHP Builder Staff
on January 16, 2009

PHP Code:
@date_default_timezone_set("GMT"); 


class rss extends XMLWriter 
{ 
    // $file = filename to output to 
    // $title = rss fed title 
    // $description = rss feed description 
    // $link = rss feed link 
    // $date = date of feed 
     
    function __construct($file, $title, $description, $link, $date) 
    { 
        // Start by calling the XMLWriter constructor... 
         
        $this->openURI($file); 
        $this->startDocument('1.0'); 
        $this->setIndent(4); 
         
        $this->startElement('rss'); 
            $this->writeAttribute('version', '2.0'); 
            $this->writeAttribute('xmlns:atom', 'http://www.w3.org/2005/Atom'); 

        $this->startElement("channel"); 
            $this->writeElement('title', $title); 
            $this->writeElement('description', $description); 
            $this->writeElement('link', $link); 
            $this->writeElement('pubDate', date("D, d M Y H:i:s e", strtotime($date))); 
         
    } 
     
    // Send a multi-dimensional array 
    // $array = 
    // 'title' = Item title 
    // 'descritpion' 
    // 'link' 
    // 'guid' = unique id for item (should be a url) 
     
    // 'category' = array of: 
    // 'title' 
    // 'domain' 
     
    function addItem($array) 
    { 
        if (is_array($array)) 
        { 
            $this->startElement("item"); 
            $this->writeElement('title', $array['title']); 
            $this->writeElement('link', $array['link']); 
            $this->writeElement('description', $array['description']); 
            $this->writeElement('guid', $array['guid']); 
             
            if (isset($array['date'])) 
            { 
                $this->writeElement('pubDate', date("D, d M Y H:i:s e", strtotime($array['date']))); 
            } 
             
            if (isset($array['category']) && isset($array['category']['title'])) 
            { 
                $this->startElement('category'); 
                    $this->writeAttribute('domain', $array['category']['domain']); 
                    $this->text($array['category']['title']); 
                $this->endElement(); // Category 
            } 
        $this->endElement(); // Item 
        } 
         
    } 
     
    function _endRss() 
    { 
        // End channel 
        $this->endElement(); 
         
        // End rss 
        $this->endElement(); 
         
        $this->endDocument(); 
         
        $this->flush(); 
    } 
     
} // end class... 

…and a sample on the class usage:

PHP Code:
// Sample usage of class... 

$item = array(); 
$item['title'] = 'New product One'; 
$item['link'] = 'http://www.domain.com/product1.htm'; 
$item['description'] = 'A full description of product that is new.'; 
$item['guid'] = 'http://www.domain.com/product1.htm'; // a unique http address will do! 
$item['date'] = date('Y-m-d'); //send any time of date! 

$item['category'] = array(); 
$item['category']['title'] = 'CD Players'; 
$item['category']['domain'] = 'http://www.domain.com/cdplayers.htm'; 

$w = new rss('php://output', 'New Products', 'This month's new products', 'http://www.domain.com/link.htm', date('Y-m-d')); 
     
$w->addItem($item); 

$w->_endRss(); 

You could use a destructor, but IMO, I think the ‘_endRss’ is better. Next project… PHP XMLReader!