PHP, XML and Wrappers

Hi guys,

Weird PHP/XML issue happening here.

I have a php script that parses XML files of football team statistics and outputs team leaders, specific stats and more. The XML files get delivered automatically to my server. (I’ve picked this site up from someone else).

The XML files used to get wrapped with a wrapper of <xts:sports-content-set> and </xts:sports-content-set>. Some server changes later, and they don’t. And they won’t be.

When the xml files have the <xts> wrapper, the php works like magic, sorting all the data as necessary. When they don’t have the wrapper, the php doesn’t do jack.

But, there doesn’t seem to be anything in the php script that identifies this xts wrapper. Which is the weird part.

Here’s the relevant code. What am I missing here? Why is it working with the wrapper (which I can’t see anywhere here) and not without it?

   // determine stats file to use
        $fileToUse = '';
        $highestId = 0;
        if ($handle = opendir($XMLPath)) {
            while ($thisFilename = readdir($handle)) {
                $fileParts = explode('.', $thisFilename);
                if (end($fileParts) != 'xml') continue;
                $fileParts = explode('-', $fileParts[1]);
                if ($fileParts[1] == 'teamstats') // filetype validation
                    if (strtolower($selectedTeam['abbr']) == strtolower($fileParts[2])) // team validation
                        if (intval($fileParts[0]) > $highestId) { // time validation
                            $fileToUse = $thisFilename;
                            $highestId = intval($fileParts[0]);
        if ($fileToUse != '') {
            $statsXML = file_get_contents($XMLPath . $fileToUse);

This works fine - it pulls the correct XML file depending on team and the latest file sent to the server.

Here’s one example of how the stats are pulled:

<?php // team/players stats modules
    if ($statsXML != '') {
        $xml = new SimpleXMLElement($statsXML);

        // team stats module
            $offensiveStats = $xml->{'sports-content'}->statistic->team->{'team-stats'}->{'team-stats-american-football'}->{'stats-american-football-offensive'};
            $passingYards = number_format((string) $offensiveStats->{'stats-american-football-passing'}['passes-yards-gross']);
            $passingPct = (string) $offensiveStats->{'stats-american-football-passing'}['passes-percentage'];
            $passingTD = (string) $offensiveStats->{'stats-american-football-passing'}['passes-touchdowns'];
            $passingInt = (string) $offensiveStats->{'stats-american-football-passing'}['passes-interceptions'];
            $rushingAtt = (string) $offensiveStats->{'stats-american-football-rushing'}['rushes-attempts'];
            $rushingYards = number_format((string) $offensiveStats->{'stats-american-football-rushing'}['rushes-yards']);
            $rushingAvg = (string) $offensiveStats->{'stats-american-football-rushing'}['rushes-average-yards-per-game'];
            $rushingTD = (string) $offensiveStats->{'stats-american-football-rushing'}['rushes-touchdowns'];
            $receivingRec = (string) $offensiveStats->{'stats-american-football-passing'}['receptions-total'];
            $receivingYards = number_format((string) $offensiveStats->{'stats-american-football-passing'}['receptions-yards']);
            $receivingAvg = (string) $offensiveStats->{'stats-american-football-passing'}['passes-average-yards-per-game'];

Here’s a clip of the top of the XML files, delivered without the xts wrapper:

<sports-content xmlns:xts="" xmlns:str="java.lang.String" xmlns:dt="" xmlns:exsl="" path-id="american-football/" xts:systemid="NFL_Team_Stats_XML" xts:tsnid="12091699">
  <sports-metadata xmlns:fs="" date-time="20101004T001600-0400" doc-id="xt.12091699-teamstats-nyg" xts:tsnslug="ABX%TEAMSTATS-NYG" language="en-US" revision-id="" fixture-key="season-stats" document-class="statistics" fixture-name="Season Stats">

The key here is that everything works great if I manually add the <xts> wrapper to the XML file, but not without it. (The files get pushed automatically and regularly and updating manually is not an option).

Any ideas?

Without knowing more about your XML file, it appears that changing the following line:

$offensiveStats = $xml->{'sports-content'}->statistic->team->{'team-stats'}->{'team-stats-american-football'}->{'stats-american-football-offensive'};


$offensiveStats = $xml->statistic->team->{'team-stats'}->{'team-stats-american-football'}->{'stats-american-football-offensive'};

Might solve your issue. The logic being that the “sports-content” is now the root node of the XML file rather than one step deep.

You, sir, are a genius. That worked perfectly!

Thank you :slight_smile: