Location: PHPKode > scripts > Google SiteMap generator > google-sitemap-generator/xmlsitemap-documentation.txt
Generating XML Sitemap what support Google, Yahoo and MSN

This is a XML Sitemap which is supposed to be processed by search engines like Google, MSN Search and YAHOO.

You can find more information about XML sitemaps on sitemaps.org and Google's list of sitemap programs.

An submited url for search engines looks like this line of code:

Where tag <loc> are web page url what would be indexed by search engine.  The <lastmod> tag tells search engines the date when this document was modified.  And tag <changefreq> tells search engines about change frequency of document. With <priority> tag we can change the priority for indexing, accepted values are between 0 and 1 (ex: 0.1, 0.6).

XML header is:

<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="sitemap.xsl"?><urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

So let’s try to generate with PHP this XML file. I will write a class for that.
<?php
class SiteMap(){
	var $file; //file name for xml will be writen.
	var $pages = array(); //This array will collect all information about Url


	function SiteMap($_file){
		$this->file = $_file;
	}

	function addPage($url, $changefreq = 'daily', $priority = 1.0){
		$this->pages['url'][] = $url;
		$this->pages['changefreq'][] = $changefreq;
		$this->pages['priority'][] = $priority;
	}

	function write2file($fname, $string)
	{
		@unlink($fname);
		@file_put_contents($fname, $string);
	}
	function getPages()
	{
		for ($i = 0; $i < count($this->pages['url']); $i ++){
		$str .= '
		<url>
		   <loc>'.$this->pages['url'][$i].'</loc>
		   <lastmod>'.date('Y-m-d').'T'.date('H:i:s').'+00:00</lastmod>
		   <changefreq>'.$this->pages['changefreq'][$i].'</changefreq>
		   <priority>'.$this->pages['priority'][$i].'</priority>
		</url>';
		}
		return $str;
	}
	function xmlHeader()
	{
		$str = '<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="sitemap.xsl"?><urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
		return $str;
	}
	function xmlFooter()
	{
		$str = '
		</urlset>
		';
		return $str;
	}


	function create()
	{
		$str = $this->xmlHeader();
		$str .= $this->getPages();
		$str .= $this->xmlFooter();
		$this->write2file($this->file, $str);
		die('Done! <a href="sitemap.xml">SiteMap.xml</a>');
	}
}

?>&#8232;
Here is an example how to use this class.

$map = new SiteMap(‘sitemap.xml’);
$map->addPage(‘http://my-site.com/page.html’, 'daily', 1);
$map->addPage(‘http://my-site.com/page2.html’, 'daily', 0.8);
$map->addPage(‘http://my-site.com/page3.html’, 'daily', 0.7);
$map->create();

If we have an multiple urls stored into an array so the code will be:

$urls = array(); // here are stored our urls.

$map = new SiteMap(‘sitemap.xml’);
for($i = 0; $i < count($urls); $i ++){
	$map->addPage($urls[$i][‘url’], $urls[$i][‘changefreq’], $urls[$i][‘ priority’]);
}
$map->create();
<url>
    <loc>http://my-web-site.com/</loc>
    <lastmod>2008-11-25T23:24:30+00:00</lastmod>
    <changefreq>daily</changefreq>
    <priority>1</priority>
</url>
Return current item: Google SiteMap generator