2011-08-09 173 views
0

我需要PHP代碼,它將從XML字符串中返回一個包含玩家統計信息的數組。我認爲Xpath將需要使用。從XML獲取玩家統計信息

<playerStats> 
    <player> 
    <blocksDestroyed>1485</blocksDestroyed> 
    <blocksPlaced>1882</blocksPlaced> 
    <creatureKills>13</creatureKills> 
    <currency>0</currency> 
    <deaths>7</deaths> 
    <isOnline>false</isOnline> 
    <itemsDropped>1507</itemsDropped> 
    <lastLogin>1312908744</lastLogin> 
    <metersTraveled>19236</metersTraveled> 
    <playerGroups>Ops</playerGroups> 
    <playerKills>0</playerKills> 
    <playerName>Joe</playerName> 
    <playerSince>1312776719</playerSince> 
    <sessionPlaytime></sessionPlaytime> 
    <sessionPlaytimeSeconds>-1</sessionPlaytimeSeconds> 
    <totalPlaytime>5.16 hours</totalPlaytime> 
    </player> 
    <player>Another player's stats</player>... 
</playerStats> 

我需要傳遞的<player></player>節點,在<playerName></playerName>$_GET['name']

回答

1

使用SimpleXML,你的XPath查詢是要看起來像

$players = $xml->xpath(
     sprintf('//player[playerName = "%s"]', 
       htmlentities($_GET['name'], ENT_QUOTES, 'UTF-8') 
     )); 

if (count($players)) { 
    $player = $players[0]; 
}