Get XML document attributes

0

I tried to get the elements of the <facts> tag when it enters the cycle it does not show me any output, try the children() and attributes() .

<?xml version="1.0" encoding="UTF-8"?>
    <Facts away_team_id="368" away_team_name="France" competition="European Championship Finals" competition_id="3" game_date="2016-07-10 20:00:00" game_id="838557" home_team_id="359" home_team_name="Portugal" lang_id="es" last_modified="2016-07-07 22:02:42" matchday="7" season="Season 2015/2016" season_id="2015" sport_id="1" sport_name="Football" state="pre">
    <message id="1" fact="Este ser&amp;aacute; el cuarto encuentro entre Portugal y Francia en un gran torneo. Los franceses han ganado los tres anteriores, todos en semifinales (EURO 1984 EURO 2000, Copa Mundial 2006). " />
    
asked by Paulo Mayoral 01.10.2018 в 18:12
source

1 answer

0

EYE: Your XML has serious problems of validation and structure, I recommend you check a reference before the following: link

You can do it by means of the attribute index:

$xml=simplexml_load_file("tuarchivo.xml") or die("Error: Cannot create object");
echo $xml->Facts[0]['away_team_id'] . "<br>";
echo $xml->Facts[0]->title['away_team_name'];

It will produce the output:

368
France

To do it with a for:

$xml=simplexml_load_file("tuarchivo.xml") or die("Error: Cannot create object");
foreach($xml->children() as $facts) { 
    echo $facts['away_team_id'];
    echo "<br>"; 
} 
    
answered by 01.10.2018 в 19:38