Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP生成xml foreach_Php_Xml_Foreach - Fatal编程技术网

PHP生成xml foreach

PHP生成xml foreach,php,xml,foreach,Php,Xml,Foreach,我想根据一些数据生成一个xml文件,我的代码是: <?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 <

我想根据一些数据生成一个xml文件,我的代码是:

<?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();
}

?>


数据存储在test.xml中,工作正常,但只显示1组信息(在标记匹配内,我想要多个,用于多个游戏),如何在标记“匹配”内显示多个xml数据?当我看到以这种方式创建的xml而不是使用
SimpleXML
addChild()时,我会感到害怕
等。查看如何操作。将
foreach
放在
之间。。。要获得更详细的答案,请发布
$fixtures
@michi中的最低工作示例,当我尝试这样做时,xml文件返回空的….@michi在fixtures中您有xml格式的游戏信息,如:334064 2015-05-02T15:00:00+00:00 Ligue 1 35您能稍微减少示例并将其作为fixture吗?我没有XMLSoccer API,所以这很难说。但有了一些演示数据,这应该是直截了当的。