Barnaby Norman

RSS Gerorator Source Code

Barnaby Norman Home Page | Site Map | Family Norman Genealogy Site | Contact Me

RSS Generator Source Code

The RSS Generator is another application based on the file menu.xml. The application reads through the file taking the relevant items and adds them to a dom object before converting it to XML.

Part of rssGenerator.php showing the php code:

<?php

// Create new dom object
$dom new DomDocument('1.0''utf-8');

// Create the RSS element
$rss $dom->appendChild(new DOMElement('rss'));
$rss->setAttributeNode(new DOMAttr('version''2.0'));

// Add the channel to hold all the feeds
$channel $rss->appendChild(new DOMElement('channel'));

// Create main channel items:
// Main feed title
$title $channel->appendChild($dom->createElement('title'));
$title->appendChild($dom->createTextNode("Barnaby Norman's Web Portfolio")); 

// Main feed home page link
$link $channel->appendChild($dom->createElement('link'));
$link->appendChild($dom->createTextNode("http://www.deta-it.co.uk/")); 

// Main feed site description
$description $channel->appendChild($dom->createElement('description'));
$description->appendChild($dom->createTextNode("A website designed to highlight significance and expertise in areas of the work of Barnaby Norman")); 

// Main feed language
$language $channel->appendChild($dom->createElement('language'));
$language->appendChild($dom->createTextNode("en-uk")); 

// Load the xml from the menu xml file
$xml simplexml_load_file("../siteStuff/menu.xml");

// Loop through the xml adding text nodes to the channel object
foreach($xml->menuItem as $xmlItem)
{
    
$subXml $xmlItem;

    foreach(
$subXml->menuItem as $subItem)
    {        
        
// Create item for each sub xml item
        
$item $channel->appendChild($dom->createElement('item'));

        
// Title
        
$title $item->appendChild($dom->createElement('title'));
        
$title->appendChild($dom->createTextNode($subItem->title->a)); 

        
// Link
        
$link $item->appendChild($dom->createElement('link'));
        
$link->appendChild($dom->createTextNode("http://www.deta-it.co.uk" $subItem->title->a->attributes())); 

        
//Test for description and add if available
        
if($subItem->description)
        {
            
$description $item->appendChild($dom->createElement('description'));
            
$description->appendChild($dom->createTextNode($subItem->description));
        }

        
// Published date and time
        
$pubDate $item->appendChild($dom->createElement('pubDate'));
        
$pubDate->appendChild($dom->createTextNode($subItem->lastmod)); 
    }
}

// Convert the dom object into xml
$dom->formatOutput true
$xmlSitemap $dom->saveXML();

// Write to a file
$file = fopen("rss.xml""w");
fwrite($file, $xmlSitemap);
fclose($file);

// Present to display
echo "<pre class=\"code\">" . htmlentities($xmlSitemap) . "</pre>";

?>