Site Map Listing Tutorial
Page 2
Site Map Listing Tutorial - Page 1
The changes I made to Kevin's script are minor - namely I changed the tag names that are parsed in the switch statment in the characterData function. I also changed the output line in the endElement function to strip off the site name from the page URL. Showing a link as
tutorials/sitemap/listing/page-2.shtml
seemed more aesthetic than showing the whole URL, as in
http://www.mikrosight.com/tutorials/sitemap/listing/page-2.shtml
Other changes could be made to strip the extensions, or like my second site map, I chose to read the file and extract the <title> tag from the page and use that information in my display. More on that later.
class RSSParser {
var $insideitem = false;
var $loc = "";
var $priority = "";
var $changefreq = "";
var $RSSFile = "";
function startElement($parser, $tagName, $attrs) {
if ($this->insideitem) {
$this->tag = $tagName;
}
elseif ($tagName == "URL") {
$this->insideitem = true;
}
}
function endElement($parser, $tagName) {
if ($tagName == "URL") {
echo "<li><a href='", trim($this->loc), "'>", htmlspecialchars(substr($this->loc, strpos($this->loc, '/', 8)+1)),"</a></li>";
$this->loc = "";
$this->priority = "";
$this->changefreq = "";
$this->insideitem = false;
}
}
function characterData($parser, $data) {
if ($this->insideitem) {
switch ($this->tag) {
case "LOC":
$this->loc .= $data;
break;
case "PRIORITY":
$this->description .= $data;
break;
case "CHANGEFREQ":
$this->link .= $data;
break;
}
}
}
}
$xml_parser = xml_parser_create();
$rss_parser = new RSSParser();
$rss_parser->RSSFile = "http://www.mikrosight.com/sitemap.xml";
xml_set_object($xml_parser,&$rss_parser);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen($rss_parser->RSSFile, 'r')
or die("Error reading RSS data.");
echo '<ul>';
while ($data = fread($fp, 4096))
xml_parse($xml_parser, $data, feof($fp))
or die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
echo '</ul>';
fclose($fp);
xml_parser_free($xml_parser);
?>