
SteffenBaier
Novice
Mar 15, 2010, 7:59 AM
Post #1 of 5
(657 views)
|
|
[Question] Reading CSV File and convert into XML File
|
Can't Post
|
|
Hello all, I am now trying to read a CSV File and parse the output formatted as XML. My code so far: #!/usr/bin/perl $i=0; #print "(<?xml version="1.0" encoding="UTF-8" standalone="yes"?>)\n"; print "<directory>\n"; print " <item_list>\n"; print " <item>\n";open (FILE, '<C:/Temp/test.txt'); while (<FILE>) { chomp; ($lastname, $firstname, $phone) = split("\,"); $i++; print " <ln>$lastname</ln>\n"; print " <ln>$firstname</ln>\n"; print " <ct>$phone</ct>\n"; print " <sd>$i</sd>\n"; print " </item>\n"; } print " </item_list>\n"; print "</directory>\n";open CTI, ">C:/temp/directory-000000000000.xml"; print CTI<<eof ; $s<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?> $s<!-- \$RCSfile: 000000000000-directory~.xml,v \$ \$Revision: 1.3 \$ --> $s<directory> $s <item_list> $s </item> $s <ln>$lastname</ln> $s <fn>$firstname</fn> $s <ct>$phone</ct> $s <sd>$i</sd> $s </item> $s </item_list> $s</directory>eof close CTI; close (FILE); exit; The Display Print shows the expected Output: <directory> <item_list> <item> <ln>doe</ln> <ln>joe</ln> <ct>1001</ct> <sd>1</sd> </item> <ln>Parker</ln> <ln>Jane</ln> <ct>1002</ct> <sd>2</sd> </item> <ln>Smith</ln> <ln>bill</ln> <ct>1003</ct> <sd>3</sd> </item> </item_list> </directory> But the final Directory File only contains the 3rd record <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <!-- $RCSfile: 000000000000-directory~.xml,v $ $Revision: 1.3 $ --> <directory> <item_list> </item> <ln>Smith</ln> <fn>bill</fn> <ct>1003</ct> <sd>3</sd> </item> </item_list> </directory> Any Ideas? Steff
(This post was edited by SteffenBaier on Mar 15, 2010, 8:04 AM)
|