我想使用 php 创建多个 XML 文件。我使用了以下代码,该代码对于单个 XML 文件生成工作正常,但是当我尝试在循环中使用相同的代码来生成多个具有不同名称的 XML 文件时,它会抛出错误,但文件保存在目标驱动程序中,谁能帮我解决这个问题
$fileName = date('YmdHis').rand('0000','999999')."output.xml";
单次通话工作正常
xmlGenerater($pimcoArr ,$fileName);
当我在循环中调用它时抛出错误
for($=0;$i<2;$i++){
xmlGenerater($pimcoArr ,$fileName);
}
错误信息屏幕
XML文件生成方法
function xmlGenerater($data ,$fileName){
if(!empty($data)){
$writer = new XMLWriter();
//lets store our XML into the memory so we can output it later
$writer->openMemory();
//lets also set the indent so its a very clean and formatted XML
$writer->setIndent(true);
//now we need to define our Indent string,which is basically how many blank spaces we want to have for the indent
$writer->setIndentString(" ");
//Lets start our document,setting the version of the XML and also the encoding we gonna use
//XMLWriter->startDocument($string version, $string encoding);
$writer->startDocument("1.0", "UTF-8");
//lets start our main element,lets call it “ersvpresponse”
$writer->startElement('ersvpresponse');
$loop = 1;
foreach($data as $dataRow){
$pimco_id = $dataRow['pimco_id'];
$event_id = $dataRow['event_id'];
$contact_id = $dataRow['contact_id'];
$status = $dataRow['status'];
$writer->startElement("contact");
if($loop==1){
$dateTime = date('Y-m-d');
$writer->writeAttribute("updated",$dateTime);
}
//now we create pimcoeventid node
$writer->startElement("pimcoeventid");
$writer->text("$pimco_id");
$writer->endElement();
//now we create pimcocontactid node
$writer->startElement("pimcocontactid");
$writer->text("$contact_id");
$writer->endElement();
//now we create pimcocontactid node
$writer->startElement("ersvpstatus");
$writer->text("$status");
$writer->endElement();
$writer->endElement();
$loop++;
}
//Now lets close our main element
$writer->endElement();
//close our document
$writer->endDocument();
/*Lets output what we have so far,first we need to set a header so we can display the XML in the
browser,otherwise you will need to look at the source output. */
header('Content-type: text/xml');
//lets then echo our XML;
//echo $writer->outputMemory();
/* that is enough to display our dynamic XML in the browser,now if you wanna create a XML file we need to do this */
$filename = "exportFiles/$fileName";
//lets output the memory to our file variable,and we gonna put that variable inside the file we gonna create
$file = $writer->outputMemory();
//lets create our file
file_put_contents($filename,$file);
}
}