Sitemap class

simple class for generating xml sitemap

stáhnout snippet
zobrazení: SyntaxHighlighter | GeSHi | Holý text
<?php

class Sitemap {
  
  private $xml;
  private $urlset;
  
  public function __construct() {
    $this->xml = new DOMDocument('1.0', 'utf-8');
    $this->xml->formatOutput = true;
    
    $this->urlset = $this->xml->createElement('urlset');
    $this->urlset->setAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
    
    $this->xml->appendChild( $this->urlset );
  }

  public function addUrl($loc, $lastmod = null, $changefreq = null, $priority = null) {
    
    $url = $this->xml->createElement('url');
    
    $item = $this->xml->createElement('loc');
    $item->appendChild($this->xml->createTextNode( $loc ));
    $url->appendChild($item);
    
    if($lastmod) {
      $item = $this->xml->createElement('lastmod');
      $item->appendChild($this->xml->createTextNode( $lastmod ));
      $url->appendChild($item);
    }
    
    if($changefreq) {
      $item = $this->xml->createElement('changefreq');
      $item->appendChild($this->xml->createTextNode( $changefreq ));
      $url->appendChild($item);
    }
    
    if($priority) {
      $item = $this->xml->createElement('priority');
      $item->appendChild($this->xml->createTextNode( $priority ));
      $url->appendChild($item);
    }
  
    $this->urlset->appendChild( $url );
  }
  
  public function out() {
    return $this->xml->saveXML(); 
  }
}


Tagy:
php 51 řádků | 2008-04-06 16:07:23 | air.kadlec@seznam.cz