M_A_K
M_A_K

Reputation: 378

XMLWriter using with loop in php

i want to create multiple XML file by using php. i used following code which is working fine for single XML file generation but when i try to same code in loop for generation multiple XML file with different name its throw an error but file is save in destination driver can anyone help me to solve this issue

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

working fine for single calling

xmlGenerater($pimcoArr ,$fileName);

error throw when i call it in loop

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

error message screen

enter image description here

XML file genartion method

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


    }
}

Upvotes: 0

Views: 1192

Answers (1)

VolkerK
VolkerK

Reputation: 96159

You are putting the documents in separate files on the server but you send them to the client as one single repsonse document, hence the

Line Number 14
<?xml ...

and that doesn't work, you can't just concatenate xml documents and expect the client-side parser to accept that.
But you could e.g. zip those files and send the archive.

Upvotes: 1

Related Questions