Search code examples
phpxmldomsimplexml

How to store XML child nodes in PHP variables through a given parent ID?


<account VGS0035="VGS0035">
        <realm>Smolderweb</realm>
        <realmint>smolderweb-us</realmint>
        <type>PvP</type>
        <race>Undead</race>
        <gender>Male</gender>
        <class>Mage</class>
        <faction>Horde</faction>
        <level>60</level>
        <description>(60% mount, green/blue gear)</description>
        <price>210</price>
        <stock>1</stock>
        <id>VGS0035</id>
        <screenshot>https://vanilla.games/wp-content/uploads/2019/08/wow-classic-mage.jpg</screenshot>
    </account>

    <account VGS0036="VGS0036">
        <realm>Faerlina</realm>
        <realmint>faerlina-us</realmint>
        <type>PvP</type>
        <race>Undead</race>
        <gender>Male</gender>
        <class>Mage</class>
        <faction>Horde</faction>
        <level>60</level>
        <description>(100% mount, epic/blue gear, Tailoring 300, First Aid 225, MC+ONY+BWL attuned, 150 gold)</description>
        <price>400</price>
        <stock>1</stock>
        <id>VGS0036</id>
        <screenshot>https://i.imgur.com/cdeAdwe.jpg</screenshot>
    </account>

    <account VGS0037="VGS0037">
        <realm>Faerlina</realm>
        <realmint>faerlina-us</realmint>
        <type>PvP</type>
        <race>Undead</race>
        <gender>Male</gender>
        <class>Mage</class>
        <faction>Horde</faction>
        <level>60</level>
        <description>(60% mount, green/blue/epic gear, 100 gold)</description>
        <price>250</price>
        <stock>1</stock>
        <id>VGS0037</id>
        <screenshot>https://vanilla.games/wp-content/uploads/2019/08/wow-classic-mage.jpg</screenshot>
    </account>

I have the following wrapped in XML tags. I have a PHP page where i pass a variable page?id=VGS0003 - I want to extract all of that node's child elements in PHP variables for use.

Example: 

$realm = $account->realm;
$region = $account-region;
$race = $account->race;

I've tried almost every method but nothing seems to be working. Foreach only returns the last node no matter if i place if statements within the loop.

                $accountID = $_GET['id'];
                

                $xmlurl = "../config/classic/accounts.php";
                include($xmlurl);
                $packages = new SimpleXMLElement($xmlstr);

                foreach($packages->accounts->account as $account){

                    if($accountID == $account->id){

                    $accountRace = $account->race;
                    $accountRaceLowerU = strtolower($accountRace);
                    $accountRaceLowerU = str_replace(' ', '_', $accountRaceLowerU);
                    $accountGender = $account->gender;
                    $accountGenderLower = strtolower($accountGender);
                    $accountClass = $account->class;
                    $accountClassLower = strtolower($accountClass);
                    $accountFaction = $account->faction;
                    $accountScreenshotThumb = $account->images->image[0];
                    $accountScreenshot = $account->screenshot;
                    $accountSKU = $account->id;
                    $accountLevel = $account->level;
                    $accountRealm = $account->realm;
                    $accountType = $account->type;

                    }

                };

How to accomplish this? What I am looking to do is rather simple, yet it seems i've tried everything. Find the account node through the ID, and save the child element values as php variables to be used elsewhere on the page.


Solution

  • One option could be using xpath "//account[id='$id']" with the id that you are trying to find.

    $elements = simplexml_load_string($xmlstr);
    $id = "VGS0037";
    $query = "//account[id='$id']";
    $account = $elements->xpath($query);
    $accountRace = $account[0]->race;
    echo $accountRace;
    

    Output

    Undead
    

    Php demo