CSV to XML

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
berry8549
Forum Newbie
Posts: 1
Joined: Tue Jul 24, 2007 2:45 am

CSV to XML

Post 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.
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

should be something like this

Post 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
Post Reply