•
•
•
•
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
![]() |
hey any body know more about this script to convert CSV to XML
Thank you
<?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
•
•
Join Date: May 2008
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
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
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
•
•
Join Date: May 2008
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
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.
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?
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb XML, XSLT and XPATH Marketplace
- CSV to XML (Visual Basic 4 / 5 / 6)
- Porblem in UTF-8 support on CSV/EXCEL (PHP)
- Parsing a csv file in C (C)
- over heating (Cases, Fans and Power Supplies)
- Processor (Windows NT / 2000 / XP / 2003)
Other Threads in the XML, XSLT and XPATH Forum
- Previous Thread: TreeView using XAML
- Next Thread: tell me pleasee!!


Linear Mode