0

我正在尝试从每个 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;

知道anoyne我如何搜索元素中的字符串吗?

提前谢谢,对不起,我只是一个初学者

4

1 回答 1

0

如果你想检查和比较提供的针($joke),你可以使用一个简单的 foreach 来检查。要进行比较,您可以使用__toString()或简单的 typecast (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();
    }
}
于 2014-06-01T13:36:38.553 回答