Aanshi
Aanshi

Reputation: 282

XML to CSV converter using API

I want to convert my XML file into CSV file but i want to use any tool who convert it automatically and i can pass input parameter using its API.I don't want to use any script to directly convert XML to csv. Is there any way to perform this?

Upvotes: 1

Views: 1211

Answers (1)

kreya
kreya

Reputation: 1229

Use PHP_curl....

Here is the code..

<?php
$filexml="formdata.xml";
 if (file_exists($filexml))            {
   $xml = simplexml_load_file($filexml);
    $f = fopen('invoice.csv', 'w');
    createCsv($xml, $f);
    fclose($f);
  }
function createCsv($xml,$f)
{
 $arr1 = array('col1','col2'...); 
 fputcsv($f, $arr1 ,',','"');
     foreach ($xml->record as $item) 
    {
       $hasChild = (count($item->record) > 0)?true:false;

    if( ! $hasChild)
    {
    //item is xml tag name 
        $put_arr = array($item->company_name); 

     fputcsv($f, $put_arr ,',','"');

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

}
?>

Upvotes: 2

Related Questions