hey any body know more about this script to convert CSV to XML

<?php
/**
 * Converts a CSV file to a simple XML file
 *
 * @param string $file
 * @param string $container
 * @param string $rows
 * @return string
 */

error_reporting(E_ALL ^ E_NOTICE);
ini_set("display_errors", true);

function csv2xml($file, $container = 'data', $rows = 'row')
{
        $r = "<{$container}>\n";
        $row = 0;
        $cols = 0;
        $titles = array();
        
        $handle = @fopen($file, 'r');
        if (!$handle) return $handle;
        
        while (($data = fgetcsv($handle, 1000, ',')) !== FALSE)
        {
             if ($row > 0) $r .= "\t<{$rows}>\n";
             if (!$cols) $cols = count($data);
             for ($i = 0; $i < $cols; $i++)
             {
                  if ($row == 0)
                  {
                       $titles[$i] = $data[$i];
                       continue;
                  }
                  
                  $r .= "\t\t<{$titles[$i]}>";
                  $r .= $data[$i];
                  $r .= "</{$titles[$i]}>\n";
             }
             if ($row > 0) $r .= "\t</{$rows}>\n";
             $row++;
        }
        fclose($handle);
        $r .= "</{$container}>";
        
        return $r;
}
$xml = csv2xml('/var/www/html/media/xml/DS20080507.csv', 'export', 'feedPublishDate');




?>

Thank you

Hi,

This function use fgetcsv function, it's look like it's not included on your code, and it's not a standard php function.

It's look like it's return an array of data containing each column values. If it's true, it will produce an xml file using first row of csv as element title and the content as value for each rows.

Hope it's help

Hi,

This function use fgetcsv function, it's look like it's not included on your code, and it's not a standard php function.

It's look like it's return an array of data containing each column values. If it's true, it will produce an xml file using first row of csv as element title and the content as value for each rows.

Hope it's help

I modify the code to this and it works

<?php
/**
* Converts a CSV file to a simple XML file
*
* @param string $file
* @param string $container
* @param string $rows
* @return string
*/

error_reporting(E_ALL ^ E_NOTICE);
ini_set("display_errors", true);


function csv2xml($file, $container = 'data', $rows = 'row')
{

  $r = "<{$container}>\n";
  $row = 0;
  $cols = 0;
  $titles = array();

  $handle = @fopen($file, 'r');
  if (!$handle) return $handle;

  while (($data = fgetcsv($handle, 1000, ',')) !== FALSE)
  {
    if ($row > 0) $r .= "\t<{$rows}";
    if (!$cols) $cols = count($data);
    for ($i = 0; ($i < $cols) && (strlen($data[$i]) > 0); $i++)
    {
      if ($row == 0)
      {
        $titles[$i] = $data[$i];
        continue;
      }
      
      if ($titles[$i] == "Description")
      {
        $r .= ">\n\t\t<description>";
        $r .= $data[$i];
        $r .= "</description>\n";
      }
      elseif ($titles[$i] == "ID")
        $r .= " adID=\"{$data[$i]}\"";
      elseif ($titles[$i] == "Category")
        $r .= " catID=\"{$data[$i]}\"";
    }
    if ($row > 0) $r .= "\t</{$rows}>\n";
    $row++;
  }
  fclose($handle);
  $r .= "</{$container}>";

  return $r;


}



$xml = csv2xml('DS20080507.csv', 'export', 'ad');

$xmlfile = @fopen('DS20080507.xml', 'wb') or die('Could not open XML file for writing');

fwrite($xmlfile, $xml) or die('Could not write string to XML file');

fclose($xmlfile);

echo "Successfully wrote the XML file";




?>

Now the problem is that my csv file does not have the headers ID, Description , Category in the top of the file. I wrote this code

<?php


$list = array
(
"ID,Category,Description,,,,,,,,,,,,,,,,,,"
);

$file = fopen("DS20080507.csv","r+", 1000);

foreach ($list as $line)
  {
  fputcsv($file,split(',',$line));
  }

fclose($file);



$row = 1;
$handle = fopen("DS20080507.csv", "r+");


while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $num = count($data);
   
    $row++;
    for ($c=0; $c < $num; $c++) {
        echo $data[$c] . "<br />\n";

    }
}




fclose($handle);


?>

But it does not work right, it puts the headers in the top but deletes part of the first line of the csv file. I ve tries usine file mode a and a+ and puts the headrs in the bottom of the csv

any ideas on how to fix this issue?

Thank you

Well, if you don't have the titles in your files it's a different problem you have to do a new function based on the first one with customizable titles. The trade off is you have to know the number of cols in your file.

Remove this :

if ($row > 0) -- keep the rest

and this

 if ($row == 0)
                  {
                       $titles[$i] = $data[$i];
                       continue;
                  }

And the new prototype should be something like :

function csv2xmlCustTitles($file, $titles, $container = 'data', $rows = 'row')

The call would be something like :

$titles = array('ID','Category','Description');
$xml = csv2xmlCustTitles('DS20080507.csv', $titles 'export', 'ad');

It's not perfect but it's would work.

Well, if you don't have the titles in your files it's a different problem you have to do a new function based on the first one with customizable titles. The trade off is you have to know the number of cols in your file.

Remove this :

and this

And the new prototype should be something like :

The call would be something like :

It's not perfect but it's would work.

I have that working the problem is now in trying to insert the headers to my csv file to have an automated process

here is the code

<?php


$list = array
(
"ID,Category,Description"
);

$file = fopen("DS20080507.csv","r+");

foreach ($list as $line)
  {
  fputcsv($file,split(',',$line));
  }

fclose($file);



$row = 1;
$handle = fopen("DS20080507.csv", "r+");


while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $num = count($data);
   
    $row++;
    for ($c=0; $c < $num; $c++) {
        echo $data[$c] . "<br />\n";

    }
}




fclose($handle);


?>

It writes the headers the problem is that is deletes part of my csv first line

do you a way to fix this?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.