PHP Classes

File: xmlsitemap-documentation.txt

Recommend this page to a friend!
  Classes of Botnari Constantin  >  Google SiteMap generator  >  xmlsitemap-documentation.txt  >  Download  
File: xmlsitemap-documentation.txt
Role: Documentation
Content type: text/plain
Description: This is documentation for sitemap class
Class: Google SiteMap generator
Generate XML sitemaps from page lists
Author: By
Last change:
Date: 9 years ago
Size: 3,487 bytes


Class file image Download
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 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="" xsi:schemaLocation="" xmlns="">

So let’s try to generate with PHP this XML file. I will write a class for that.
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)
		@file_put_contents($fname, $string);
	function getPages()
		for ($i = 0; $i < count($this->pages['url']); $i ++){
		$str .= '
		return $str;
	function xmlHeader()
		$str = '<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="sitemap.xsl"?><urlset xmlns:xsi="" xsi:schemaLocation="" xmlns="">';
		return $str;
	function xmlFooter()
		$str = '
		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>');

Here is an example how to use this class.

$map = new SiteMap(‘sitemap.xml’);
$map->addPage(‘’, 'daily', 1);
$map->addPage(‘’, 'daily', 0.8);
$map->addPage(‘’, 'daily', 0.7);

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’]);