Jump to content

Unusual xml file


Recommended Posts

I am not sure why this just isn't coming to me but for some reason it's stumping how to properly parse this thing!!  


Every line has it's own entry and trying to get the info for each match is proving somewhat puzzling for me! I can get any of the info but how for instance can I get both teams together since each team has it's own line and each match has 2 lines in it....   I'm trying to pull the data then make a table out of it but for some reason it's just escaping me totally.... 




That is the xml file...


$team1 = $games->attributes()->id;   that is how I am getting it but it return both teams..... how can I do this line by line?  Any suggestions would be helpful!



Link to comment
Share on other sites

I think your confusion stems from the fact that the "teams" are separate objects within an array of the match. There may be another way, but it will work if you assign those two array elements of the main object to two new variables/objects, then extract the data there. Also, you can reference the attributes as you have above OR via array indexes which is a little simpler, IMHO.



//Load the xml content into object
$xmlObj = simplexml_load_file('test.xml');
//Output the week
echo "NFL Scheduled Week: {$xmlObj->attributes()->week}<br><br>\n";
foreach($xmlObj as $matchup)
    //Display the matchup attributes
    echo "<b><u>Matchup attributes</u></b><br>";
    echo "Kickoff: {$matchup->attributes()->kickoff}<br>\n"; //Object property
    echo "Game Seconds Remaining: {$matchup['gameSecondsRemaining']}<br><br>\n"; //Array index
    //Get the team objects from the team array
    $team1 = $matchup->team[0];
    $team2 = $matchup->team[1];
    //Display team1 attributes as object properties
    echo "<b>Team 1:</b><br>\n";
    echo "</ul>";
    echo "<li>ID: {$team1->attributes()->id}</li>\n";
    echo "<li>isHome: {$team1->attributes()->isHome}</li>\n";
    echo "<li>score: {$team1->attributes()->score}</li>\n";
    echo "<li>rushDefenseRank: {$team1->attributes()->rushDefenseRank}</li>\n";
    echo "<li>passDefenseRank: {$team1->attributes()->passDefenseRank}</li>\n";
    echo "<li>rushOffenseRank: {$team1->attributes()->rushOffenseRank}</li>\n";
    echo "<li>passOffenseRank: {$team1->attributes()->passOffenseRank}</li>\n";
    echo "<li>hasPossession: {$team1->attributes()->hasPossession}</li>\n";
    echo "<li>inRedZone: {$team1->attributes()->inRedZone}</li>\n";
    echo "<li>spread: {$team1->attributes()->spread}</li>\n";
    echo "</ul><br>";
    //Display team2 attributes as array indexes
    echo "<b>Team 2:</b><br>\n";
    echo "</ul>";
    echo "<li>ID: {$team2['id']}</li>\n";
    echo "<li>isHome: {$team2['isHome']}</li>\n";
    echo "<li>score: {$team2['score']}</li>\n";
    echo "<li>rushDefenseRank: {$team2['rushDefenseRank']}</li>\n";
    echo "<li>passDefenseRank: {$team2['passDefenseRank']}</li>\n";
    echo "<li>rushOffenseRank: {$team2['rushOffenseRank']}</li>\n";
    echo "<li>passOffenseRank: {$team2['passOffenseRank']}</li>\n";
    echo "<li>hasPossession: {$team2['hasPossession']}</li>\n";
    echo "<li>inRedZone: {$team2['inRedZone']}</li>\n";
    echo "<li>spread: {$team2['spread']}</li>\n";
    echo "</ul><br><hr>\n";





NFL Scheduled Week: 19

Matchup attributes
Kickoff: 1358026200
Game Seconds Remaining: 0

Team 1:

  • ID: BAL
  • isHome: 0
  • score: 38
  • rushDefenseRank: 20
  • passDefenseRank: 19
  • rushOffenseRank: 10
  • passOffenseRank: 12
  • hasPossession: 0
  • inRedZone: 0
  • spread:
  • Team 2:

  • ID: DEN
  • isHome: 1
  • score: 35
  • rushDefenseRank: 4
  • passDefenseRank: 6
  • rushOffenseRank: 15
  • passOffenseRank: 5
  • hasPossession: 0
  • inRedZone: 0
  • spread:

 . . . etc . . .

Link to comment
Share on other sites

OK, after taking a look afterwards, here is another way without needing to create new objects for each team.


Team 1 ID:

$matchup->team[0]->attributes()->id OR $matchup->team[0]['id']


Team 2 ID:

$matchup->team[1]->attributes()->id OR $matchup->team[1]['id']

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.