PHP and XML parsing

Aravinthan

New Member
Messages
68
Reaction score
0
Points
0
Hi, I used this code before but for some reason when I changed the elemts of it, it stopped working. So here is the part that goes before html tag:
Code:
<?php
 
$xml_rank_key = "*RATINGS*CLANRATING*RANK";
$xml_rate_key = "*RATINGS*CLANRATING*RATE";
$xml_active_key = "*RATINGS*CLANRATING*ACTIVE";
$story_array = array();
$counter = 0;
class xml_story{
    var $rank, $rate, $active;
}
function startTag($parser, $data){
    global $current_tag;
    $current_tag .= "*$data";
}
function endTag($parser, $data){
    global $current_tag;
    $tag_key = strrpos($current_tag, '*');
    $current_tag = substr($current_tag, 0, $tag_key);
}
function contents($parser, $data){
    global $current_tag, $xml_rank_key, $xml_rate_key, $xml_active_key, $counter, $story_array;
    switch($current_tag){
        case $xml_rank_key:
            $story_array[$counter] = new xml_story();
            $story_array[$counter]->rank = $data;
            break;
        case $xml_rate_key:
            $story_array[$counter] = new xml_story();
            $story_array[$counter]->rate = $data;
            break;
        case $xml_active_key:
            $story_array[$counter]->active = $data;
            $counter++;
           break;
    }
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startTag", "endTag");
xml_set_character_data_handler($xml_parser, "contents");
$fp = fopen("[URL]http://www.elorating.com/ranking-services/ratingquery?name=wrwr&mode=SP&laddertype=51&game=3[/URL]", "r") or die("Could not open file");

$data = fread($fp, 4096)  or die("Could not read file");
if(!xml_parse($xml_parser, $data))
{
    die("Error on line " . xml_get_error_code($xml_parser));
}
xml_parser_free($xml_parser);
fclose($fp);
And this is the code that outputs the results:
Code:
<table>
 <?php
for($x=0;$x<count($story_array);$x++){

  echo "<tr><td>Rank: </td>" ;
  echo "<td>" . $story_array[$x]->rank . "</td></tr>";
  echo "<tr><td>Rate: </td>" ;
  echo "<td>" . round($story_array[$x]->rate, 4) . "</td></tr>";
  echo "<tr><td>Active: </td>" ;
  echo "<td>" . $story_array[$x]->active . "</td></tr>";

}
?>
</table>
But for some reason the rank array doesnt show, or shows empty, the rate and active array shows their data.....
How come?
Thanks for your help...
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
Rank is being set, but the next time contents() is called you reset $story_array[$counter] to a new xml_story object when setting the rate. So you need to modify the contents() function to this:

PHP:
function contents($parser, $data){
    global $current_tag, $xml_rank_key, $xml_rate_key, $xml_active_key, $counter, $story_array;
    switch($current_tag){
        case $xml_rank_key:
            $story_array[$counter] = new xml_story();
            $story_array[$counter]->rank = $data;
            break;
        case $xml_rate_key:
            $story_array[$counter]->rate = $data;
            break;
        case $xml_active_key:
            $story_array[$counter]->active = $data;
            $counter++;
           break;
    }
}

However this assumes that the order of the elements will stay with rank always preceding the other two. If the order is subject to change, then I would recommend using more reliable code.
 

Aravinthan

New Member
Messages
68
Reaction score
0
Points
0
OK thank, but now I decided to add 2 more things: Name and Abbrev. Here is the top code:
Code:
<?php



$xml_name_key = "*RATINGS*CLANRATING*NAME";
$xml_abbrev_key = "*RATINGS*CLANRATING*ABBREV";
$xml_rank_key = "*RATINGS*CLANRATING*RANK";
$xml_rate_key = "*RATINGS*CLANRATING*RATE";
$xml_active_key = "*RATINGS*CLANRATING*ACTIVE";

$story_array = array();

$counter = 0;
class xml_story{
    var $name, $abbrev, $rank, $rate, $active;
}

function startTag($parser, $data){
    global $current_tag;
    $current_tag .= "*$data";
}

function endTag($parser, $data){
    global $current_tag;
    $tag_key = strrpos($current_tag, '*');
    $current_tag = substr($current_tag, 0, $tag_key);
}

function contents($parser, $data){
    global $current_tag, $xml_rank_key, $xml_rate_key, $xml_active_key, $counter, $story_array;
    switch($current_tag){
        case $xml_name_key:
            $story_array[$counter] = new xml_story();
            $story_array[$counter]->name = $data;
            break;
         case $xml_abbrev_key:
			 $story_array[$counter]->abbrev = $data;
			 break;
        case $xml_rank_key:
            $story_array[$counter]->rank = $data;
            break;
        case $xml_rate_key:
            $story_array[$counter]->rate = $data;
            break;
        case $xml_active_key:
            $story_array[$counter]->active = $data;
            $counter++;
           break;
    }
}

$xml_parser = xml_parser_create();

xml_set_element_handler($xml_parser, "startTag", "endTag");

xml_set_character_data_handler($xml_parser, "contents");

$fp = fopen("http://www.elorating.com/ranking-services/ratingquery?name=wrwr&mode=SP&laddertype=51&game=3", "r") or die("Could not open file");


$data = fread($fp, 4096)  or die("Could not read file");

if(!xml_parse($xml_parser, $data))
{
    die("Error on line " . xml_get_error_code($xml_parser));
}

xml_parser_free($xml_parser);

fclose($fp);

?>
And here is the code that displays:
Code:
<table>
	<?php
for($x=0;$x<count($story_array);$x++){
  echo "<tr><td>Name: </td>" ;
  echo "<td>" . $story_array[$x]->name . "</td></tr>";
  echo "<tr><td>Abbrev: </td>" ;
  echo "<td>" . $story_array[$x]->abbrev . "</td></tr>";
  echo "<tr><td>Rank: </td>" ;
  echo "<td>" . $story_array[$x]->rank . "</td></tr>";
  echo "<tr><td>Rate: </td>" ;
  echo "<td>" . round($story_array[$x]->rate, 4) . "</td></tr>";
  echo "<tr><td>Active: </td>" ;
  echo "<td>" . $story_array[$x]->active . "</td></tr>";


}
?>
</table>
THe Name and abrrev doenst show, but rank,rate and active does -.-
THanks for your help
 
Last edited:
Top