Skip to content

Instantly share code, notes, and snippets.

@devster
Created March 21, 2012 17:21
Show Gist options
  • Save devster/2149765 to your computer and use it in GitHub Desktop.
Save devster/2149765 to your computer and use it in GitHub Desktop.
Helper sitemap index xml
<?php
/**
* SiteMapIndexXML
*
* A simple helper to make simple sitemap.xml of index
*
* Usage:
* $sitemap = new SiteMapIndexXML();
* $sitemap->addSiteMap('http://example.org/sitemap-products.xml');
* $sitemap->saveXML() // returns the string xml
*
* @author Jeremy Perret <[email protected]>
*/
class SiteMapIndexXML extends \DOMDocument
{
public $root;
public function __construct($charset = 'UTF-8')
{
parent::__construct('1.0', $charset);
$this->root = $this->appendChild($this->createElement('sitemapindex'));
$xmlns = $this->createAttribute('xmlns');
$xmlns->value = 'http://www.sitemaps.org/schemas/sitemap/0.9';
$this->root->appendChild($xmlns);
}
public function addSiteMap($url)
{
$urlNode = $this->createElement('sitemap');
$this->root->appendChild($urlNode);
$urlNode->appendChild($this->_createTextNode('loc', $url));
}
protected function _createTextNode($name, $value)
{
$el = $this->createElement($name);
$el->appendChild($this->createTextNode($value));
return $el;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment