asha80 has asked for the wisdom of the Perl Monks concerning the following question:

Hi i have one script(Scriptone) where i am generating xml file.Here i am putting values direclty in the script. for ex "ReqID" => "18AEXR" Now what i need to do is i need to pick value from excel and put it into respective field. i have written one code which will read data from excel. Now my problem is how to merge these two scripts script one:
#!/usr/bin/perl use XML::Writer; use IO::File; my $output = new IO::File('>C:\Documents and Settings\ashalatha\Desk +top\PEG\Perl scripting\xml\Files\output1.xml'); my $writer = new XML::Writer(OUTPUT => $output); $writer->startTag("FIXML"); $writer->startTag("PosMntReq", "ReqID" => "18AEXR" ,"TxnTyp" => "1", +"BizDt"=>"2003-12-04", "SetSesID" => "EOD", "TxnTm" => "2003-09-10T00:00:00"); $writer->endTag("PosMntReq"); $writer->endTag("FIXML"); $writer->end(); $output->close();
script two:
#!/usr/bin/perl -w use strict; use Spreadsheet::ParseExcel; use Data::Dumper; # cobbled together from examples for the Spreadsheet::ParseExcel and # Spreadsheet::WriteExcel modules my $sourcename = 'C:\Documents and Settings\la\Desktop\EuronextTestDat +a.xls'; #shift @ARGV or die "invocation: $0 <source file>"; my $source_excel = new Spreadsheet::ParseExcel; my $source_book = $source_excel->Parse($sourcename) or die "Could not open source Excel file $sourcename: $!"; my $storage_book; foreach my $source_sheet_number (0 .. $source_book->{SheetCount}-1) { my $source_sheet = $source_book->{Worksheet}[$source_sheet_number]; print "-----SHEET:", $source_sheet->{Name}, "\n"; # sanity checking on the source file: rows and columns should be sens +ible next unless defined $source_sheet->{MaxRow}; next unless $source_sheet->{MinRow} <= $source_sheet->{MaxRow}; next unless defined $source_sheet->{MaxCol}; next unless $source_sheet->{MinCol} <= $source_sheet->{MaxCol}; foreach my $row_index (0 .. $source_sheet->{MaxRow}) { foreach my $col_index (0 .. $source_sheet->{MaxCol}) { my $source_cell = $source_sheet->{Cells}[$row_index][$col_index]; if ($source_cell) { #print "( $row_index , $col_index ) =>", $source_cell->Value, "\n" +; $storage_book->{$source_sheet->{Name}}->{$row_index}->{$col_index} = + $source_cell->Value; } # end of source_cell check } # foreach col_index } # foreach row_index } # foreach source_sheet_number print "Perl recognized the following data (sheet/row/column order):\n" +; print Dumper( $storage_book ); print "Excel Content read successfully!\n";

Replies are listed 'Best First'.
Re: Reading data from excel and create xml file using data
by baxy77bax (Deacon) on Jun 12, 2009 at 10:54 UTC
    i see you used the code tags but slightly incorrect, you are missing the frontslash in the end code tag -> '<'/code'>'

    Update:

    well it would help if i new something about data your structure. Personally i rarly use xml and xls, only when there is no other options (i like my data in csv, tsv...) but looking at your scripts all you should do is initialize writer then xls and in the part where it states

    my $source_cell = $source_sheet->{Cells}[$row_index][$col_index]; if ($source_cell) { # here you add data from xls into xml (not just copy/past the line + like i did but you have to know the structure of your data thah you +wish to pass into xml) #$writer->startTag("PosMntReq", "ReqID" => "18AEXR" ,"TxnTyp" => " +1","BizDt"=>"2003-12-04","SetSesID" => "EOD", "TxnTm" => "2003-09-10T +00:00:00"); #$storage_book->{$source_sheet->{Name}}->{$row_index}->{$col_index +} = $source_cell->Value; } # end of source_cell check
    just state that output of your xls should be input to xml. and that should be it . in the end you close your xml
    $writer->endTag("PosMntReq"); $writer->endTag("FIXML"); $writer->end(); $output->close();