0

I want to generate a xml file based in some data my code is :

<?php
include("test.php");

try{

$soccer=new XMLSoccer("api key");
$fixtures=$soccer->GetLiveScore();
foreach($fixtures as $key=>$value){         

echo "Match id <br>" .$value->Id. "<br> hometeam <br>".$value-    >HomeTeam. "<br>hometeamid <br>".$value->HomeTeam_Id. "<br>" .$value-  >HomeGoals."<br>".$value->AwayGoals. "<br> awayteamid<br>".$value->AwayTeam_Id. "<br>" .$value->AwayTeam. "<br>" .$value->Date. "<br>".$value->Time."<br>".$value->League."<br>".$value->Stadium."<br><br> <br>";

$xml="<instagoal>\n\t\t";
{

$xml .="<match>\n\t\t";
$xml .= "<id>".$value->Id."</id>\n\t\t";
$xml .= "<date>".$value->Date."</date>\n\t\t";
$xml .= "<league>".$value->League."</league>\n\t\t";
$xml .= "<round>".$value->Round."</round>\n\t\t";
$xml .= "<hometeamid>".$value->HomeTeam_Id."</hometeamid>\n\t\t";
$xml .= "<hometeamname>".$value->HomeTeam."</hometeamname>\n\t\t";
$xml .= "<awayteamid>".$value->AwayTeam_Id."</awayteamid>\n\t\t";
$xml .= "<awayteamname>".$value->AwayTeam."</awayteamname>\n\t\t";
$xml .= "<time>".$value->Time."</time>\n\t\t";
$xml .= "<hometeamgoals>".$value->Home_Goals."  </hometeamgoals>\n\t\t";
$xml .= "<awayteamgoals>".$value->Away_Goals." </awayteamgoals>\n\t\t";
$xml .= "<stadium>".$value->Location."</stadium>\n\t\t";

$xml.="</match>\n\t";
}

$xml.="</instagoal>\n\r"; 

$xmlobj=new SimpleXMLElement($xml);
$xmlobj->asXML("test.xml");

}   
}
catch(XMLSoccerException $e){
echo "XMLSoccerException: ".$e->getMessage();
}

?>

The data is stored in test.xml and works fine, but only shows 1 group of information (inside tag match, i want multiple, for multiple games), how can i do a foreach to show multiple xml data inside the tag "match" ?

  • It makes me cringe when I see XML created that way instead of using `SimpleXML`'s `addChild()` and the like. See http://stackoverflow.com/questions/143122/using-simplexml-to-create-an-xml-object-from-scratch how to do it. – michi May 02 '15 at 18:37
  • Put the `foreach` between `` and ``... For a more detailed answer, please post a minimum working sample of what is in `$fixtures` – michi May 02 '15 at 18:39
  • @michi , when i try to do that the xml file returns empty .... – Saul Henriques May 02 '15 at 18:47
  • @michi in fixtures you have game info in xml like: 334064 2015-05-02T15:00:00+00:00 Ligue 1 35 – Saul Henriques May 02 '15 at 18:51
  • for one or for more matches? Is your goal to have many matches in your XML instead of only one? – michi May 02 '15 at 18:53
  • for multiple matches, for one it's done however the source data have multiple matches and i want all of them in my test.xml file, and i only get one for now, because of the foreach i think... – Saul Henriques May 02 '15 at 19:24
  • XML file returns empty? That's perhaps an error. Get error messages and you know if that's the issue: [How to get useful error messages in PHP?](http://stackoverflow.com/q/845021/367456) – hakre May 02 '15 at 20:11
  • Only returns empty when i put the foreach between and i think i define the foreach wrong to that kind of data foreach($fixtures as $key=>$value) – Saul Henriques May 02 '15 at 20:36
  • 1
    Can you reduce the example a bit and have it a fixture? I don't have an XMLSoccer soccer API so this is hard to talk about. But with some demo-data this should be straight forward. – hakre May 03 '15 at 07:03

0 Answers0