CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
[Question] Reading CSV File and convert into XML File

 



SteffenBaier
Novice

Mar 15, 2010, 7:59 AM

Post #1 of 5 (1066 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:


Code
 #!/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:


Code
  <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


Code
  <?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)


toolic
User

Mar 15, 2010, 9:23 AM

Post #2 of 5 (1058 views)
Re: [SteffenBaier] [Question] Reading CSV File and convert into XML File [In reply to] Can't Post

You need to move your "print CTI" inside your while loop.


SteffenBaier
Novice

Mar 15, 2010, 9:32 AM

Post #3 of 5 (1057 views)
Re: [toolic] [Question] Reading CSV File and convert into XML File [In reply to] Can't Post

Hi,

I am stuck with your answer ;-)

Put it behind the while command or inside the (<FILE>) ..?



Sorry


SteffenBaier
Novice

Mar 15, 2010, 3:36 PM

Post #4 of 5 (1046 views)
Re: [SteffenBaier] [Question] Reading CSV File and convert into XML File [In reply to] Can't Post

I am going to try and read up here

http://linuxgazette.net/issue65/okopnik.html

if no one beats me to it

THX

Steff


SteffenBaier
Novice

Mar 18, 2010, 7:14 AM

Post #5 of 5 (1025 views)
Re: [toolic] [Question] Reading CSV File and convert into XML File [In reply to] Can't Post

sorry,

I still cannot follow you ..Unsure

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives