<?php
    
/**
     * Example RSS Feed
     * By Jason Strese <blog.jstrese.net>
     */
    
    
$rss = new DOMDocument('1.0');
    
$rss->formatOutput true;
    
    
// <rss></rss>
    
$rootnode $rss->createElement('rss');
    
$rootnode->setAttribute('xmlns:atom''http://www.w3.org/2005/Atom');
    
$rootnode->setAttribute('version''2.0');

    
// <channel>
    
$channelnode $rss->createElement('channel');
    
        
// <title></title>
        
$channelnode->appendChild($rss->createElement('title''Example RSS Feed'));
        
        
// <description></description>
        
$channelnode->appendChild($rss->createElement('description' 'A mock-up RSS feed.'));
        
        
// <lastBuildDate></lastBuildDate>
        
$channelnode->appendChild($rss->createElement('lastBuildDate' 'Sun, 21 Sep 2008 18:56 -0500'));
        
        
// <language></language>
        
$channelnode->appendChild($rss->createElement('language' 'en-us'));
        
        
// <ttl></ttl>
        
$channelnode->appendChild($rss->createElement('ttl' '60'));
    
        
// -> atom:link
            
$atomlink $rss->createElement('atom:link');
            
$atomlink->setAttribute('href''http://www.examplesite.com/feed/news.rss');
            
$atomlink->setAttribute('rel''self');
            
$atomlink->setAttribute('type''application/rss+xml');
        
// <- channnelnode
        
$channelnode->appendChild($atomlink);
        
    
// </channel>
    
    //
    // $news would generally be replaced by an array of news, perhaps from a database?
    //
    
$news = array(
                    array(
                            
'guid' => '32424',
                            
'title' => 'Article 32424 - Dog Eats Man',
                            
'author' => 'John Doe',
                            
'pubDate' => 'Sun, Sep 21st 2008',
                            
'description' => '<strong>A dog, ate a man - wow!</strong>'
                        
),
                    array(
                            
'guid' => '32425',
                            
'title' => 'Article 32425 - Mans Revenge, Eats Dog',
                            
'author' => 'John Doe',
                            
'pubDate' => 'Sun, Sep 21st 2008',
                            
'description' => '<strong>Some man has gotten revenge on a dog, by eating it.</strong>'
                        
)
                );
    
    
//
    // Now we're taking the results from $news, and populating the channel element with item elements
    // .. and population the item elements with even more elements ..
    //
    // Fun stuff.
    //
    
foreach($news as $item)
    {
        
// <item>
            
$itemnode $rss->createElement('item');
            
// -> guid
                
$guid $rss->createElement('guid'$item['guid']);
                
$guid->setAttribute('isPermaLink''false');
            
// <- itemnode
            
$itemnode->appendChild($guid);
            
            
// <link></link>
            
$itemnode->appendChild($rss->createElement('link''http://www.examplesite.com/article/'.$item['guid']));
            
            
// <title></title>
            
$itemnode->appendChild($rss->createElement('title'$item['title']));
            
            
// <author></author>
            
$itemnode->appendChild($rss->createElement('author'$item['author']));
            
            
// <pubDate></pubDate>
            
$itemnode->appendChild($rss->createElement('pubDate'$item['pubDate']));
            
            
// <description><![CDATA[ ]]></description>
            
$description $rss->createElement('description');
            
$description->appendChild($rss->createCDATASection($item['description']));
            
$itemnode->appendChild($description);
        
// </item>
        
        // Give this item element to the channel
        
$channelnode->appendChild($itemnode);
    }
    
    
// Give the channel node, and all its content, to the root (rss) node
    
$rootnode->appendChild($channelnode);
    
    
// Place the entire <rss> element (containing everything) into the DOMDocument
    
$rss->appendChild($rootnode);
    
    
// Save the RSS feed - RSS feeds either have the .xml extension or .rss extension, it's your choice!
    
$rss->save('./rss.xml');
?>