Dynamically create sitemap xml using php

Hi,

I have created sitemap for my site using some reference code in the below link

http://stackoverflow.com/questions/2747801/creating-an-xml-sitemap-with-php

But I am getting error as

XML Parsing Error: undefined entity Location:

as my content is as follows

<< alt >> attribute and it says something like

< loc >http://www.example.com/700- & laquo;alt & raquo;-attributes-in-images.php< /loc >

Can anyone tell me how to get rid of this error.

Here is how I created my dynamic sitemap.xml


define('jj', '<br />');

//===============================
function _add_item($title='', $id='')
{
	// $title  = ascii_to_entities($title);
  $joke_of_the_day = 'joke/of_the_day/';
  if (empty($id)) // $id < 1)
  {
    $joke_of_the_day  = '';
    // $id     = url_title($id); // , 'underscore');
    // $title  = url_title($title, 'underscore'); // STRIPS '/' WHY ?????
    $SLASH	= ''; // NO-SLASH
  }else{
    $title  = url_title($title,'underscore');
    $SLASH	= '/';
  }
  $title = strtolower($title); // ESSENTIAL
  
  // echo jj, $title;
  $site = (LOCALHOST) ? 'http://localhost/' : 'http://johns-jokes.com/';
  $result = "<url>"
          .   "<loc>$site" .$joke_of_the_day .$title .$SLASH .$id .'</loc>'
          .   '<lastmod>2009-09-09</lastmod>'
          .		'<changefreq>weekly</changefreq>'
          .		'<priority>0.8</priority>'
          . '</url>';

/*
always. Use this value for pages that change every time they are accessed.
hourly
daily
weekly
monthly
yearly
never. Use this value for archived URLs.
*/          
          
  return $result;          
}//endfunc 

  
//===================================================
function index($first=0, $count=10) 
{
  	error_reporting(E_ALL);
  	ini_set('display_errors', 'On');
  	
    // sitmap.xml header
    $lines    = array();
    $lines[]  = "<?xml version='1.0' encoding='UTF-8'?>";
    if(FALSE)
    {
    	$lines[]  = "<urlset xmlns='http://www.google.com/schemas/sitemap/0.84' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://www.google.com/schemas/sitemap/0.84 http://www.google.com/schemas/sitemap/0.84/sitemap.xsd'>";
    }else{
			// Google Template
    	$lines[]  = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
    }


    // OPEN URL and hard-code the first line
    $lines[]  = "<url>
    							<loc>http://johns-jokes.com/</loc>
    							<lastmod>2009-09-10</lastmod>
    							<changefreq>hourly</changefreq>
    							<priority>1.0</priority>
    						</url>";
    // $lines[]  = "<url><loc>http://johns-jokes.com/</loc><lastmod>" 
    //           . date('Y-m-d') ."</lastmod><priority>1.0</priority><changefreq>daily</changefreq></url>";
              
    // special cases
    $lines[] = $this->_add_item('all-the-jokes');
    $lines[] = $this->_add_item('joke/search/iphone');
    $lines[] = $this->_add_item('joke/search/ipad');
    $lines[] = $this->_add_item('top-10');
    $lines[] = $this->_add_item('top-10-jokes');
    $lines[] = $this->_add_item('sex-jokes');
    $lines[] = $this->_add_item('blonde-jokes');
    $lines[] = $this->_add_item('chuck-norris');
    $lines[] = $this->_add_item('little-johnny');
    $lines[] = $this->_add_item('health-and-safety');
    $lines[] = $this->_add_item('favourite-jokes');
    $lines[] = $this->_add_item('funny-pictures');
    $lines[] = $this->_add_item('top-20-jokes');
    $lines[] = $this->_add_item('quotes');
    $lines[] = $this->_add_item('wisdoms');
    $lines[] = $this->_add_item('eliza');
    $lines[] = $this->_add_item('bizarre-searches');
    $lines[] = $this->_add_item('safety-at-work');

   // loop through all the jokes in reverse order    
    $sql    = 'SELECT count(*) AS joke_max FROM jokes;';
		$query  = $this->db->query($sql);
    $_SESSION['joke_max'] = $query->row()->joke_max;

    $first  = 0;  
    $count  = 22; // JUST TESTING
    if (TRUE) // the lot
    {
echo jj, 'Joke Records: ',
			$count  = $_SESSION['joke_max'];
    }
    
    $sql    = "SELECT id, title FROM jokes ORDER BY id DESC LIMIT $first, $count;";
    $query	= $this->db->query($sql);
    
    $result = array();
    if ($query->num_rows())
    {
      foreach($query->result() as $row):    
        $lines[] = $this->_add_item($row->title, $row->id);
      endforeach;  
    }

    // close the XML 
    $lines[] = '</urlset>';

		// set default path to save sitemap.xml
   	$filepath = '/home/jjokes/public_html/ci_jokes/sitemap/'; // with a trailing slash
    // chmod($filepath, 0777); // NOT REQUIRED
    if (LOCALHOST)
    {
   		$filepath = "/awww/ci_jokes/sitemap/"; // with a trailing slash
    }
   	$filename = 'sitemap.xml';
    // open and truncate file
echo jj, '$file: ', 
    $file     = fopen($filepath .$filename,"w") or exit("Unable to open file!");
    
echo jj, '$filepath .$filename: ', 
			$filepath .$filename;    
			
echo jj, 'BYTES SAVED: ', 
    file_put_contents($filepath .$filename, $lines);
echo jj; 

    fclose($file);

    if (LOCALHOST)
    {
    	// fopen($filename, 'r');
			$contents = file_get_contents($filepath .$filename, "r");
			echo jj, $contents;
	  }else{
	  	$destination = '/home/jjokes/public_html/sitemap.xml';
			$rootpath = '/home/jjokes/public_html/';

			// must move sitemap.xml manually for some unknonw reason    	
    	// chmod($rootpath, 0777); // not required
    	
echo jj, 
			123 + move_uploaded_file($filepath .$filename, $destination);
echo jj; 			
			// must move sitemap.xml manually for some unknonw reason    	
    	// chmod($rootpath, 0755); // not required
    	
    	echo jj, anchor('/' .APPPATH .'sitemap/' .$filename,'Created: ./sitemap/SiteMap.xml');
    	echo jj, anchor(base_url() .$filename,	'Copied: /siteMap.xml');
    	
echo jj,jj,
			'Must move MANUALLY', jj,jj,
			'from: <b>ci_jokes/sitemap/sitemap.xml</b>',jj,jj,
			'to: &nbsp;&nbsp; ROOT';
  	}//endif 

}//endfunc
  


.