简体   繁体   English

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

[英]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.我将以下内容包装在 XML 标签中。 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.我有一个 PHP 页面,我在其中传递了一个变量 page?id=VGS0003 - 我想在 PHP 变量中提取该节点的所有子元素以供使用。

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.无论我是否在循环中放置 if 语句,Foreach 都只返回最后一个节点。

                $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.通过ID找到account节点,将子元素值保存为php变量,在页面其他地方使用。

One option could be using xpath "//account[id='$id']" with the id that you are trying to find.一种选择可能是将 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;

Output Output

Undead

Php demo Php 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM