Site Map Generator Source Code
The Sitemap generator is used to create an XML file that the google search engines can look at to establish what pages are available on the site and their frequency of update.
It works in the same way as the side menus and the site index by looping through the menu.xml file and selecting the items required. The application begins by creating a dom object and adding nodes. The selected data from the menu.xml file is then added and the complete object is converted into XML and outputted to the screen, using the php function htmlentities() to display the tags.
The user will cut and paste the output into the sitemap.xml held in the root of the site, for google to inspect, although this could be automated and the output sent to the file.
The full source code to generate the site map
<?php
// Create new dom object
$dom = new DomDocument('1.0');
$urlset = $dom->appendChild($dom->createElement('urlset'));
$urlsetAttribute = $urlset->appendChild($dom->createAttribute("xmlns"));
$urlsetAttribute->appendChild($dom->createTextNode("http://www.sitemaps.org/schemas/sitemap/0.9"));
// Load the xml from the menu xml file
$xml = simplexml_load_file("../siteStuff/menu.xml");
// Loop through the xml adding text nodes to the dom object
foreach($xml->menuItem as $item)
{
$url = $urlset->appendChild($dom->createElement('url'));
$loc = $url->appendChild($dom->createElement('loc'));
$loc->appendChild($dom->createTextNode("http://www.deta-it.co.uk" .
$item->title->a->attributes()));
if($item->lastmod)
{
$lastmod = $url->appendChild($dom->createElement('lastmod'));
$lastmod->appendChild($dom->createTextNode($item->lastmod));
}
$subXml = $item;
foreach($subXml->menuItem as $subItem)
{
// Loop through sub items in xml
$url = $urlset->appendChild($dom->createElement('url'));
$loc = $url->appendChild($dom->createElement('loc'));
$loc->appendChild($dom->createTextNode("http://www.deta-it.co.uk" .
$subItem->title->a->attributes()));
if($subItem->lastmod)
{
$lastmod = $url->appendChild($dom->createElement('lastmod'));
$lastmod->appendChild($dom->createTextNode($subItem->lastmod));
}
}
}
// Convert the dom object into xml
$dom->formatOutput = true;
$xmlSitemap = $dom->saveXML();
// Present to display
echo "<pre class=\"code\">" . htmlentities($xmlSitemap) . "</pre>";
?>


