xml获取指定的元素

时间:2014-06-01 13:21:11

标签: php xml steam-web-api

我试图从像这样的孩子中过滤出一个元素,每个xml& PHP

XML =

-<game>   
  <gameName>ValveTestApp202990</gameName>  
  <gameVersion>7</gameVersion>   
 -<availableGameStats>
   -<achievements>
     -<achievement>
        <name>MP_MISC_1</name>
        <defaultvalue>0</defaultvalue>
        <displayName>Willkommen im Club</displayName>
        <hidden>0</hidden>
      </achievement>
     -<achievement>
        <name>MP_MISC_2</name>
        <defaultvalue>0</defaultvalue>
        <displayName>Willkommen im Penthouse</displayName>
        <hidden>0</hidden>
      </achievement>
    </achievements>    
  </availableGameStats> 
 </game>

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

$joke ="MP_MISC_2";

if ( $xml11->availableGameStats->achievements->achievement->name !=
 $joke ) continue;

知道如何在元素中搜索字符串?

非常感谢并且srry我只是一个启动者

1 个答案:

答案 0 :(得分:0)

如果您想检查并比较提供的针($joke),您可以使用简单的foreach进行检查。要进行比较,您可以使用__toString()或简单的类型转换(string) $value。考虑这个例子:

// sample file get contents
$contents = '<game>   
  <gameName>ValveTestApp202990</gameName>  
  <gameVersion>7</gameVersion>   
 <availableGameStats>
   <achievements>
     <achievement>
        <name>MP_MISC_1</name>
        <defaultvalue>0</defaultvalue>
        <displayName>Willkommen im Club</displayName>
        <hidden>0</hidden>
      </achievement>
     <achievement>
        <name>MP_MISC_2</name>
        <defaultvalue>0</defaultvalue>
        <displayName>Willkommen im Penthouse</displayName>
        <hidden>0</hidden>
      </achievement>
    </achievements>    
  </availableGameStats> 
 </game>';

$xml = simplexml_load_string($contents);
$joke ="MP_MISC_2";
foreach($xml->availableGameStats->achievements->achievement as $key => $value) {
    if($value->name->__toString() != $joke) {
        echo $value->name->__toString();
    }
}