User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the XML, XSLT and XPATH section within the Software Development category of DaniWeb, a massive community of 374,011 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,753 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our XML, XSLT and XPATH advertiser:
Views: 762 | Replies: 4
Reply
Join Date: Nov 2007
Posts: 37
Reputation: jencinas69 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
jencinas69's Avatar
jencinas69 jencinas69 is offline Offline
Light Poster

csv to xml

  #1  
May 12th, 2008
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2008
Posts: 6
Reputation: ganymede is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ganymede ganymede is offline Offline
Newbie Poster

Re: csv to xml

  #2  
May 25th, 2008
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
Reply With Quote  
Join Date: Nov 2007
Posts: 37
Reputation: jencinas69 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
jencinas69's Avatar
jencinas69 jencinas69 is offline Offline
Light Poster

Re: csv to xml

  #3  
May 27th, 2008
Originally Posted by ganymede View Post
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
Reply With Quote  
Join Date: May 2008
Posts: 6
Reputation: ganymede is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ganymede ganymede is offline Offline
Newbie Poster

Re: csv to xml

  #4  
May 27th, 2008
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.
Reply With Quote  
Join Date: Nov 2007
Posts: 37
Reputation: jencinas69 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
jencinas69's Avatar
jencinas69 jencinas69 is offline Offline
Light Poster

Re: csv to xml

  #5  
May 27th, 2008
Originally Posted by ganymede View Post
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?
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb XML, XSLT and XPATH Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the XML, XSLT and XPATH Forum

All times are GMT -4. The time now is 10:54 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC