Nicholas
Nicholas

Reputation: 171

Get Player Statistics From XML

I need PHP code which will return an array with the player's stats, from an XML string. I presume Xpath will need to be used.

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

I need to be passed the <player></player> node where <playerName></playerName> is $_GET['name'].

Upvotes: 0

Views: 126

Answers (1)

Phil
Phil

Reputation: 164914

Using SimpleXML, your XPath query is going to look something like

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

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

Upvotes: 1

Related Questions