如何通过给定的父 ID 将 XML 个子节点存储在 PHP 个变量中?

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>

我将以下内容包裹在 XML 标签中。我有一个 PHP 页面,我在其中传递了一个变量 page?id=VGS0003 - 我想在 PHP 变量中提取该节点的所有子元素以供使用。

Example: 

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

我几乎尝试了所有方法,但似乎没有任何效果。 Foreach 仅 returns 最后一个节点,无论我是否在循环中放置 if 语句。

                $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;

                    }

                };

如何实现?我想要做的很简单,但似乎我已经尝试了一切。通过ID找到account节点,将子元素值保存为php变量,在页面其他地方使用

一种选择是使用 xpath "//account[id='$id']" 和您要查找的 ID。

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

输出

Undead

Php demo