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?
Asked
Active
Viewed 1,167 times
1
-
You can use the API at convertcsv.io. See https://www.convertcsv.io/products/xml2csv. It can write to Excel too. – dataman Apr 30 '23 at 21:25
1 Answers
2
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);
}
}
}
?>

kreya
- 1,091
- 5
- 24
- 52