<div dir="ltr">Hi,<br><br>I need to convert data from xml to csv format, the data is in Hebrew.<br>I tried to do it with extracting the data by my own code and it worked, but when i try to write it to a file every variable gets a new line, even when chomping the data right before printing it to a file.<br>
<br>Here is the code:<br><br>use warnings;<br>use strict;<br>use LWP::UserAgent;<br>use utf8;<br><br>open my $source, "<", "/home/moshe/perl/work/moreshet.xml" or die "can't read file 'moreshet': $! ";<br>
open my $file, ">", "/home/moshe/perl/work/file.txt" or die "can't write file 'file.txt': $! ";<br>my $line;<br><br>my ( @links, $country, $topic, $description, $lang, $type, $mordesc, $level, $note, @items );<br>
<br>my $i = 1;<br>#my $ua = LWP::UserAgent->new;<br>#$ua->timeout(10);<br>#$ua->env_proxy;<br><br>my @header = ( 'country', 'topic', 'description', 'language', 'type', 'sub description', 'level', 'note', 'item' );<br>
<br>while ( my $entry = <$source> ) {<br> chomp $entry;<br> if ( $entry =~ /<record_country>/ ) {<br> $entry =~ s/\s+\<record_country\>//;<br> $entry =~ s/\<\/record_country\>//;<br>
$country = "$entry";<br> }<br> if ( $entry =~ /<record_topic>/ ) {<br> $entry =~ s/\s+\<record_topic\>//;<br> $entry =~ s/\<\/record_topic\>//;<br> $topic = "$entry";<br>
}<br> if ( $entry =~ /<description>/ ) {<br> $entry =~ s/\s+\<description\>//;<br> $entry =~ s/\<\/description\>//;<br> $description = "$entry";<br> }<br> if ( $entry =~ /<lang id="/ ) {<br>
$entry =~ s/\s+<lang id="\w\w">//;<br> $entry =~ s/\<\/lang\>//;<br> $lang = "$entry";<br> }<br> if ( $entry =~ /<type id="/ ) {<br> $entry =~ s/\s+<type id="\w+">//;<br>
$entry =~ s/\<\/type\>//;<br> $type = "$entry";<br> }<br> if ( $entry =~ /<sub_description>/ ) {<br> $entry =~ s/\s+\<sub_description>//;<br> $entry =~ s/\<\/sub_description\>//;<br>
$mordesc = "$entry";<br> }<br> if ( $entry =~ /<level>/ ) {<br> $entry =~ s/\s+\<level>//;<br> $entry =~ s/\<\/level\>//;<br> $level = "$entry";<br>
}<br> if ( $entry =~ /<note>/ ) {<br> $entry =~ s/\s+\<note>//;<br> $entry =~ s/\<\/note\>//;<br> $note = "$entry";<br> }<br> if ( $entry =~ /\<item/ ) {<br>
$entry =~ s/\s+\<item id\=\"\d?\d\" source\=\"//;<br> $entry =~ s/\<\/item\>//;<br> my @item = split /\"\>/, $entry;<br># my $response = $ua->get("$entry");<br>
push @links, $item[0]; # or whatever<br> push @items, $item[1];<br> }<br> if ( $entry =~ /\<\/record/ ) {<br> chomp $country;<br> chomp $topic;<br> chomp $description;<br> chomp $lang;<br>
chomp $type;<br> chomp $mordesc;<br> chomp $level;<br> print $file "$country,$topic,$description,$lang,$type,$mordesc,$level,@items,@links\n";<br> print "$country,$topic,$description,$lang,$type,$mordesc,$level,", @items, ",", @links, "\n";<br>
@links = "";<br> @items = "";<br> }<br>}<br><br>close $file;<br><br>Do you have an idea why this is not working?<br><br>Moshe<br></div>