Page 1 of 1

CSV to XML

Posted: Tue Jul 24, 2007 2:49 am
by berry8549
Hi ,

I need the code to convert a csv file to XML file .
Kindly help me in this.

Regards,
Prithvi

feyd | Split from here.

should be something like this

Posted: Wed Jul 25, 2007 3:27 pm
by yacahuma
should be something like this

Code: Select all

$lines = file('csv file here');
$xml='<DATASET>';
// Loop through our array, show HTML source as HTML source; and line numbers too.
foreach ($lines as $line_num => $line) {
   $line = str_replace(',',' ',$line); //remove any commas i thin you moight alse need to remove double quotes,maybe
   $data = split(',',$line);
   $xml .= '<DATAITEM>'
              .'<FIELD1>' . $data[0] . '</FIELD1>'
              .'<FIELD2>' . $data[1] . '</FIELD2>'
              .'<FIELD3>' . $data[2] . '</FIELD3>'
              .'</DATAITEM>';
}
$xml .= '</DATASET>';
something like this should work. be carefull will special characters like accents . xml will complain about them