XMLWriter在php中使用with循环

时间:2013-09-13 07:28:40

标签: php xml xml-parsing

我想使用php创建多个XML文件。我使用下面的代码,它可以很好地生成单个XML文件,但是当我尝试在循环中使用相同的代码生成多个具有不同名称的XML文件时,它会抛出错误,但文件保存在目标驱动程序中,任何人都可以帮助我解决此问题< / p>

$fileName   =   date('YmdHis').rand('0000','999999')."output.xml";

适用于单次通话

xmlGenerater($pimcoArr ,$fileName);
当我在循环中调用它时,

错误抛出

for($=0;$i<2;$i++){
 xmlGenerater($pimcoArr ,$fileName);
}

错误消息屏幕

enter image description here

XML文件genartion方法

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);


    }
}

1 个答案:

答案 0 :(得分:1)

您将文档放在服务器上的单独文件中,但是将它们作为单个repsonse文档发送到客户端,因此

Line Number 14
<?xml ...

这不起作用,你不能只是连接xml文档并期望客户端解析器接受它。
但你可以,例如压缩这些文件并发送存档。