Search code examples
phparraysxmlfor-loopsimplexml

PHP Array & XML Can't get all content


I'm tring to get all content from this xml: https://api.eveonline.com/eve/SkillTree.xml.aspx

To save it on a MySQL DB.

But there are some data missing...

Could any1 that understand PHP, Array() and XML help me, please?

This is my code to get the content:

<?php

    $filename = 'https://api.eveonline.com/eve/SkillTree.xml.aspx';
    $xmlbalance = simplexml_load_file($filename);
    $skills = array();

    for ($x=0;$x<sizeOf($xmlbalance->result->rowset->row);$x++) {
         $groupName = $xmlbalance->result->rowset->row[$x]->attributes()->groupName;
         $groupID = $xmlbalance->result->rowset->row[$x]->attributes()->groupID;
         for ($y=0;$y<sizeOf($xmlbalance->result->rowset->row[$x]->rowset->row);$y++) {
             $skills[$x]["skillID"] = "".$xmlbalance->result->rowset->row[$x]->rowset->row[$y]->attributes()->typeID;
             $skills[$x]["skillName"] = "".$xmlbalance->result->rowset->row[$x]->rowset->row[$y]->attributes()->typeName;            
             $skills[$x]["skillDesc"] = "".$xmlbalance->result->rowset->row[$x]->rowset->row[$y]->description;
             $skills[$x]["skillRank"] = "".$xmlbalance->result->rowset->row[$x]->rowset->row[$y]->rank;
             $skills[$x]["skillPrimaryAtr"] = "".$xmlbalance->result->rowset->row[$x]->rowset->row[$y]->requiredAttributes->primaryAttribute;
             $skills[$x]["skillSecondAtr"] = "".$xmlbalance->result->rowset->row[$x]->rowset->row[$y]->requiredAttributes->secondaryAttribute;

             $o = 0;
             for ($z=0;$z<sizeOf($xmlbalance->result->rowset->row[$x]->rowset->row[$y]->rowset->row);$z++) {
                 if ($xmlbalance->result->rowset->row[$x]->rowset->row[$y]->rowset->attributes()->name == "requiredSkills") {
                     $skills[$x]["requiredSkills"]["".$xmlbalance->result->rowset->row[$x]->rowset->row[$y]->rowset->row[$z]->attributes()->typeID] = "".$xmlbalance->result->rowset->row[$x]->rowset->row[$y]->rowset->row[$z]->attributes()->skillLevel;
                     $o++;
                 }
             }

         }

    }
     echo '<pre>'; print_r($skills); echo '</pre>';

?>

If you go to the original XML (link), at line 452, you will see:

  <row groupName="Spaceship Command" groupID="257">

And that isn't show in my array (link)...

That is one thing that i found that is missing... I think that probally have more content that is missing too..

Why? How to fix it, please?

Thank you!!!


Solution

  • You will only get a total of sizeof($xmlbalance->result->rowset->row) records. Because, in your 2nd for loop, you are basically storing your result in the same array element that is $skills[$x].

    Try this (I also higly encourage you to be as lazy as you can when you write code - by lazy I mean, avoid repeating / rewriting the same code over and over if possible) :

    $filename = 'https://api.eveonline.com/eve/SkillTree.xml.aspx';
    $xmlbalance = simplexml_load_file($filename);
    $skills = array();    
    foreach ($xmlbalance->result->rowset->row as $row)
    {
        $groupName = $row->attributes()->groupName;
        $groupID = $row->attributes()->groupID;
    
        foreach ($row->rowset->row as $subRow) 
        {
            $skill['skillID'] = (string) $subRow->attributes()->typeID;
            $skill['skillName'] = (string) $subRow->attributes()->typeName;
            $skill['skillDesc'] = (string) $subRow->description;
            $skill['skillRank'] = (string) $subRow->rank;
            $skill['skillPrimaryAtr'] = (string) $subRow->requiredAttributes->primaryAttribute;
            $skill['skillSecondAtr'] = (string) $subRow->requiredAttributes->secondaryAttribute;
    
            foreach ($subRow->rowset as $subSubRowset) 
            {
                if ($subSubRowset->attributes()->name == 'requiredSkills') 
                {
                    foreach ($subSubRowset->row as $requiredSkill) 
                    {
                        $skill['requiredSkills'][(string) $requiredSkill->attributes()->typeID] = (string) $requiredSkill['skillLevel'];
                    }
                }
            }
    
            $skills[] = $skill;
        }
    }
    print_r($skills);