"convert xml to csv with php" Code Answer

4

try this

$filexml='test.xml';

    if (file_exists($filexml)) 
           {
       $xml = simplexml_load_file($filexml);
       $f = fopen('test.csv', 'w');
       createcsv($xml, $f);
       fclose($f);
    }

    function createcsv($xml,$f)
    {

        foreach ($xml->children() as $item) 
        {

           $haschild = (count($item->children()) > 0)?true:false;

        if( ! $haschild)
        {
           $put_arr = array($item->getname(),$item); 
           fputcsv($f, $put_arr ,',','"');

        }
        else
        {
         createcsv($item, $f);
        }
     }

    }
By jtv on July 15 2022

Answers related to “convert xml to csv with php”

Only authorized users can answer the Search term. Please sign in first, or register a free account.