Beefy Boxes and Bandwidth Generously Provided by pair Networks httptech
XP is just a number
 
PerlMonks  

XML file creation based on file existence

by valavanp (Curate)
on Dec 28, 2009 at 03:03 UTC ( #814569=perlquestion: print w/ replies, xml ) Need Help??
valavanp has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,
Open a directory and read all the files, based on certain criteria the an xml file should be generated. The following example is only for 2 files whereas i have multiple files to generate the xml. The xml structure should if there is no date or no pid it should not create any tags., where as it is creating for all the dates. The directory and the file structure:
/test/filename_12345_010107_ABC.txt /test/filename_12345_010207_ABC.txt /test/filename_12345_020107_zzz.txt /test/filename_12345_020207_zzz.txt /test/filename_55555_010107_ABC.txt /test/filename_55555_010207_ABC.txt /test/filename_55555_020107_ABC.txt /test/filename_55555_020207_zzz.txt The xml file should be: <pt id="12345"> <date id="010107"> <abc id="abc" url="abc.html"></abc> <zzz id=""zzz" url="zzzz.html"></zzz> </date> </pt> <pt id="55555"> <date id="010107"> <abc id="abc" url="abc.html"></abc> </date> </pt>
The code for that:
#!/usr/bin/perl use XML::Simple; use XML::Writer; use IO::File; my %pt; my %dates; my %abcs; $dirname="test"; $output = new IO::File(">>out_file.xml"); $counter=1; opendir(DIR, $dirname) || die "Error in opening dir $dirname\n"; while(($filename = readdir(DIR))){ next if($filename eq '.' or $filename eq '..' ); ($unwanted,$unwanted,$pid,$env,$date)=split("_",$filename); chomp($date,$pid,$env); $date=~ s/^\s+//g; $date==~ s/\s+$//g; $date=~ s/\.txt//g; $projects{$pid}=[] unless exists $projects{$pid}; $dates{$date}=[] unless exists $dates{$date}; $envs{$env}=[] unless exists $envs{$env}; #push @{$projects{$pid}}, $date; #$push @{$dates{$date}}, $env; } foreach $pid(sort keys %projects){ $writer=new XML::Writer(OUTPUT => $output); if($pid==""){ }else{ $writer->startTag("pt",id=>$pid); foreach $date(sort keys %dates){ if($date==""){ }else{ $writer->startTag("date",id=>$date); foreach $env(sort keys %envs){ print $env; if($env eq ""){ }elsif($env eq "ABC"){ $writer->startTag("sit",url=>$env); $writer->endTag(); }elsif($env eq "ZZZ"){ $writer->startTag("uat",url=>$env); $writer->endTag(); } } $writer->endTag(); } } $writer->endTag(); } closedir(DIR);
The result of the xml file should be if there is no date existence, than it should not create any xml tags whereas the current code generates the xml for all dates which is wrong. The output which i am getting for the above code is
<pt id="12345"> <date id="010109"> <sit s_name="ABC"></sit> <uat u_name="ZZZ"></uat> </date> <date id="020109"> <sit s_name="ABC"></sit> <uat u_name="ZZZ"></uat> </date> </project> <project id="55555"> <date id="010109"> <sit s_name="ABC"></sit> <uat u_name="ZZZ"></uat> </date> </project>

Comment on XML file creation based on file existence
Select or Download Code
Re: XML file creation based on file existence
by Anonymous Monk on Dec 28, 2009 at 04:29 UTC

    in your code:

    • Your data are not linked to each others in any way
    • Although you used 'my' to declare some of your variables at the very top of your script, Those are now global variables which is something you do not need to do in your case.
    • Consider adding use strict; use warnings; at the top of your code
    • split function returns an array you can access it's elements by supplying the ids counting from zero
      my ($X,$Y,$Z) = (split/_/,$file)[2.3,4];
      or if you don't like using those numbers consider using undef for the element you don't need rather than using a bogus variable $unwanted <code> my (undef,undef,$X,$Y,$Z) = split /_/,$file;
    • in perl you should use 'eq' and/or 'ne' for strings comparison and I as I can see you are using numerical comparison when comparing the date to an empty string which is wrong

    Hopes those comments will help you out.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://814569]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2014-04-21 10:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (493 votes), past polls