Reputation: 339
I have the following complex, not quite well-structured XML:
<event hometeam="K.Kanepi" awayteam="S.Lisicki" eventName="K.Kanepi-S.Lisicki" date="02/07/2013" time="14:05" datetime="2013-07-02T14:05:00+02:00">
<subevent title="Match odds" dsymbol="MATCH_ODDS">
<selection name="home" odds="3.1500" slipURL="URI/en/add-bet/3187-02072013,1"/>
<selection name="away" odds="1.3500" slipURL="URI/en/add-bet/3187-02072013,2"/>
</subevent>
I'm trying to parse it using this code:
$total_games = $xml->event->count();
for ($i = 0; $i < $total_games; $i++) {
echo $xml->event[$i]->subevent.title;
$total games works correctly; however - when I try to parse subevent, I'm unsuccessful in getting the eventName, hometeam, date, etc.
Upvotes: 0
Views: 1361
Reputation: 120644
To access attributes, you need to use array subscripting instead of class member syntax. This works for me using your XML as input:
<?
$xml = simplexml_load_file('http://some.url.here/gimme.xml');
$total_games = $xml->event->count();
for ($i = 0; $i < $total_games; $i++) {
echo $xml->event[$i]->subevent['title'];
// Note the syntax here ^^^^^^^^^
}
?>
PHP's Basic SimpleXML usage page is a good resource in this case.
Upvotes: 5