Click to See Complete Forum and Search --> : XML question


jencinas69
05-29-2008, 04:07 PM
Hey I have this csv2xml parser

<?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><![CDATA[";
$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('dtifeed.csv', 'export', 'ad');

$xmlfile = @fopen('dtifeed.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";






?>
it works fine adn generate xml out put the thing is that I need to add
this to the top of the xml <xml version="1.0" encoding="Windows-1252">

Can some help with this?

Thank you

rpgfan3233
05-29-2008, 05:35 PM
Change:
$r = "<{$container}>\n";
to:
$r = "<?xml version="1.0" encoding="Windows-1252"?>\n<{$container}>\n";

That should be all that is needed. ^_^