xml通过php从children()获取单个元素

时间:2014-05-31 19:59:45

标签: php xml foreach children steam-web-api

我试图通过xml从一个用户通过php获得一个成就列表。

http://api.steampowered.com/ISteamUserStats/GetPlayerAchievements/v0001/?appid=212910&key=xxxxx&steamid=xxxxx&l=german&format=xml

XML =

-<playerstats>
 <success>true</success>
  <steamID>76561198070477917</steamID>
  <gameName>Call of Duty: Black Ops II - Zombies</gameName>
 -<achievements>
   -<achievement>
      <apiname>ZM_TRANSIT_SIDEQUEST</apiname>
      <achieved>1</achieved>
      <name>Der Turm von Gebrabbel</name>
      <description>Hören Sie in TranZit auf die Stimmen.</description>
    </achievement>
   -<achievement>
      <apiname>ZM_DONT_FIRE_UNTIL_YOU_SEE</apiname>
      <achieved>0</achieved>
      <name>Erst bei Sichtkontakt feuern</name>
      <description>Öffnen Sie in TranZit sämtliche Türen, ohne in Brand gesteckt zu werden.</description>
    </achievement>

我尝试仅列出在“1”上具有“已实现”值的元素。 我已经取得了一切成功,但现在我只想获得与实现1 -

的特殊元素

PHP =

    $url11= 'http://api.steampowered.com/ISteamUserStats/GetPlayerAchievements/v0001/?appid=' . $dasgame . '&key=' . $steam_api . '&steamid=' . $username . '&l=german&format=xml';
    $data11 = file_get_contents($url11);
    $xml11 = simplexml_load_string($data11);
    $daslade = $xml11->success;
    $daswert = "true";
    if($daslade == $daswert) { 

    foreach ($xml11->achievements->children() as $items){

    $dasladen = $items->apiname;
    print $xml8->availableGameStats->achievements->achievement->$dasladen;                      
    //print htmlentities($items->name);print "--->";
    print htmlentities($items->description);print "</br>"; };
 } else { 
print "Für dieses Spiel sind keine Errungenschaften vorhanden."; }
 };

如果您有疑问,请提前致谢,请免费提问我

1 个答案:

答案 0 :(得分:0)

如果您想跳过没有achieved元素且值为1的所有元素,您可以使用continue语句跳过剩余的处理并移动列表中的下一个项目。

foreach ( $xml11->achievements->children() as $items ){
    if ( $items->achieved != 1 ) continue;
    // ... rest of your code
}