Re: Sorting parsed XML

From: Date: Sun, 24 Nov 2002 22:18:56 +0000
Subject: Re: Sorting parsed XML
Groups: php.general 
Request: Send a blank email to php-general+get-125840@lists.php.net to get a copy of this message
I'm trying to write PHP code that will not only parse the XML but also allow me to sort the parsed information by the values parsed. Current code ========== class RSSParser { var $insideitem = false; var $tag = ""; var $ServerName = ""; var $ServerStatus = ""; var $ServerType = ""; var $ServerPopulation = ""; function startElement($parser, $tagName, $attrs) { if ($this->insideitem) { $this->tag = $tagName; } elseif ($tagName == "SERVER") { $this->insideitem = true; while (list ($key, $val) = each($attrs)) { switch ($key) { case "NAME": $this->ServerName .= $val; break; case "TYPE": $this->ServerType .= $val; break; } } } } function endElement($parser, $tagName) { if ($tagName == "SERVER") { printf("<tr>\n"); if ($this->ServerType) { printf("<td>%s <i>(%s)</i></td>\n", $this->ServerName, $this->ServerType); } else { printf("<td>%s</td>\n", $this->ServerName); } if ($this->ServerPopulation > 0) { printf("<td>%s</td>\n", $this->ServerPopulation); } else { printf("<td>%s</td>\n", $this->ServerStatus); } printf("</tr>\n"); $this->ServerName = ""; $this->ServerType = ""; $this->ServerPopulation = ""; $this->ServerStatus = ""; $this->insideitem = false; } } function serverData($parser, $data) { if ($this->insideitem) { switch ($this->tag) { case "POPULATION": $this->ServerPopulation .= $data; break; case "STATUS": $this->ServerStatus .= $data; break; } } } } $xml_parser = xml_parser_create(); $rss_parser = new RSSParser(); xml_set_object($xml_parser,&$rss_parser); xml_set_element_handler($xml_parser, "startElement", "endElement"); xml_set_character_data_handler($xml_parser, "serverData"); $fp = fopen("http://www.camelotherald.com/xml/servers.xml","r <http://www.camelotherald.com/xml/servers.xml> ") or die("Error reading RSS data."); 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))); fclose($fp); xml_parser_free($xml_parser); ============ I've reviewed many online and book references and none have been helpful in helping me to determine the way(s) in which I could do such a sort from the parse code I have found to work very well. I have tried looking for a way to also append multiple XML RSS files in one (all sharing the same structure) into one and then sort according by a value there. I am new to PHP and XML. Any help and references would be greatly appreciated. ~PHP learner

« previous php.general (#125840) next »