如何通过给定的父ID在PHP变量中存储XML子节点?
我尝试了几乎所有的方法,但似乎都不管用。无论我是否在循环中放置if语句,Foreach都只返回最后一个节点如何通过给定的父ID在PHP变量中存储XML子节点?,php,xml,dom,simplexml,Php,Xml,Dom,Simplexml,我尝试了几乎所有的方法,但似乎都不管用。无论我是否在循环中放置if语句,Foreach都只返回最后一个节点 Example: $realm = $account->realm; $region = $account-region; $race = $account->race; 如何做到这一点?我想做的很简单,但似乎我什么都试过了。通过ID找到account节点,并将子元素值保存为php变量,以便在页面上的其他位置使用。一个选项可以是使用xpath“//account[ID='
Example:
$realm = $account->realm;
$region = $account-region;
$race = $account->race;
如何做到这一点?我想做的很简单,但似乎我什么都试过了。通过ID找到account节点,并将子元素值保存为php变量,以便在页面上的其他位置使用。一个选项可以是使用xpath
“//account[ID='$ID']”
和您尝试查找的ID
$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;
}
};
输出
$elements = simplexml_load_string($xmlstr);
$id = "VGS0037";
$query = "//account[id='$id']";
$account = $elements->xpath($query);
$accountRace = $account[0]->race;
echo $accountRace;
一个选项可以是使用xpath
“//account[id='$id']”
和您试图查找的id
$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;
}
};
输出
$elements = simplexml_load_string($xmlstr);
$id = "VGS0037";
$query = "//account[id='$id']";
$account = $elements->xpath($query);
$accountRace = $account[0]->race;
echo $accountRace;
您确定您的xml吗?每个
节点都有一个与其属性值相同的属性名称?此外,xml中没有VGS0003
。您确定您的xml吗?每个
节点都有一个与其属性值相同的属性名称?另外,xml中没有VGS0003
。谢谢你,太棒了。它确实有效。非常感谢你!如果您正在观看此评论,还有一个问题-如何列出WARE REALM=FAERLINA的所有节点?我猜$realm=“Faerlina”$query=“//帐户[realm='$realm']”;这将只输出一个。如何添加它们,将它们列为“foreach”示例?所有只有领域Faerlina的节点?@Voya7您可以这样做$query=“//帐户[./realm[normalize-space()='Faerlina']]”;foreach($elements->xpath($query)as$item){echo$item->realm.PHP\u EOL;echo$item->race.PHP\u EOL;}
请参阅。你让我省去了不少头痛!非常感谢。标记为接受。两种解决方案都很完美。谢谢你,太棒了。它确实有效。非常感谢你!如果您正在观看此评论,还有一个问题-如何列出WARE REALM=FAERLINA的所有节点?我猜$realm=“Faerlina”$query=“//帐户[realm='$realm']”;这将只输出一个。如何添加它们,将它们列为“foreach”示例?所有只有领域Faerlina的节点?@Voya7您可以这样做$query=“//帐户[./realm[normalize-space()='Faerlina']]”;foreach($elements->xpath($query)as$item){echo$item->realm.PHP\u EOL;echo$item->race.PHP\u EOL;}
请参阅。你让我省去了不少头痛!非常感谢。标记为接受。两种解决方案都是完美的。