<?PHPerror_reporting(E_all|e_strict);Ini_set(' Display_errors ',true);Ini_set(' Auto_detect_line_endings ',true);$inputFilename= ' Input.csv ';$outputFilename= ' Output.xml ';//Open csv to read$inputFile=fopen($inputFilename, ' RT ');//Get The headers of the file$headers=Fgetcsv($inputFile);//Create A new DOM document with pretty formatting$doc=NewDomDocument ();$doc->formatoutput =true;//Add A root node to the document$root=$doc->createelement (' Rows '));$root=$doc->appendchild ($root);//Loop through each row creating a <row> node with the correct data while(($row=Fgetcsv($inputFile)) !==FALSE){ $container=$doc->createelement (' Row '); foreach($headers as $i=$header) { $child=$doc->createelement ($header); $child=$container->appendchild ($child); $value=$doc->createtextnode ($row[$i]); $value=$child->appendchild ($value); } $root->appendchild ($container);}Echo $doc->savexml ();
[PHP] csv to XML