从XML获取播放器统计信息

时间:2011-08-09 22:48:06

标签: php xml

我需要PHP代码,它将从XML字符串返回一个包含播放器统计数据的数组。我认为需要使用Xpath。

<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 个答案:

答案 0 :(得分:1)

使用SimpleXML,您的XPath查询看起来像

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

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