in reply to modify perl 'excel to xml'

Hello cibien, and welcome to the Monastery!

I can’t really help you with your problem, since I don’t understand what you are trying to achieve. You ask:

is possible to do the same for propriety?

but I have no idea what this means. Some examples — input and desired output — would be useful for explaining what you want to do.

Also, you should supply some sample input for the existing code, together with the output it produces, so the monks can experiment on the code and verify that it still does what it should. See How do I post a question effectively?. Also specify the version of Perl you are using. (From the line system("cls"); it appears the script is running on Windows.)

I gather you inherited the code you posted from someone else. You should be aware there are a few issues with this code as it stands:

But the real problem with the code posted is that it’s just — well — too long and complicated to wade through. In other words, it’s in desperate need of refactoring.

To make a start: the main (i.e., top-level) code could usefully be re-written like this:

{ system("cls"); # Read command line arguments my ($materialmapping_file, $data_folder) = @ARGV; print "OPTIONS\nmaterialmapping_file: $materialmapping_file\n" . "data_folder: $data_folder\n"; if (length $materialmapping_file && length $data_folder) { opendir(my $dir, $data_folder) or die "Error in opening dir $data_folder\n"; my $materialmapping_table_xml = XML::LibXML->createDocument('1.0', 'UTF-8'); my $materialmapping_table_xml_root = $materialmapping_table_xml->createElement('masterdata'); $materialmapping_table_xml_root-> setAttribute('version', getSQLTimeStamp()); $materialmapping_table_xml-> setDocumentElement($materialmapping_table_xml_root); print "Parsing files...\n"; while (my $filename = readdir($dir)) { if ($filename =~ / \. xls $ /ix) { parse_xls_file($data_folder, $filename, $materialmapping_table_xml, $materialmapping_table_xml_root); } } $materialmapping_table_xml->toFile($materialmapping_file, 2); } }

which calls a new subroutine:

sub parse_xls_file { my ($data_folder, $filename, $materialmapping_table_xml, $materialmapping_table_xml_root) = @_; if (my $oBook = Spreadsheet::ParseExcel-> new()->parse($data_folder . $filename)) { for (0 .. $oBook->{SheetCount} - 1) { process_sheet($oBook, $_, $materialmapping_table_xml, $materialmapping_table_xml_root) } } print "Parsed $filename\n"; }

which in turn calls:

sub process_sheet { my ($oBook, $iSheet, $materialmapping_table_xml, $materialmapping_table_xml_root) = @_; my $oWkS = $oBook->{Worksheet}[$iSheet]; ... }

... and so on. This process should be continued until the code becomes manageable, at which point it will be much easier to add new features, etc.

Sorry I can’t provide the solution you wanted; but I hope the above at least gives you some useful pointers.

Update: There are a couple of other issues with the original code:

The input also requires a special format: see not only the post by Anonymous Monk below, but also the comments at the head of the code in the original post by cibien. The following format works when saved as an “Excel 97-2003 Workbook (*.xls)”:

| A | B | C | D | E | ===+========+==========+===========+=========+=========+ 1 | | Configurator mapping | ---+--------+----------+-----------+---------+---------+ 2 | active | internal | pr_family | pr1 | pr2 | ---+--------+----------+-----------+---------+---------+ 3 | yes | item 1 | 1,2 | value 1 | value 2 | ---+--------+----------+-----------+---------+---------+ 4 | yes | item 2 | 1,2 | value 3 | value 3 | ---+--------+----------+-----------+---------+---------+ 5 | yes | item 3 | 1,2 | value 5 | value 4 | ---+--------+----------+-----------+---------+---------+

When this spreadsheet is processed by the script with the fixes applied, it produces the following output:

<?xml version="1.0" encoding="UTF-8"?> <masterdata version="2012-07-27 01:51:57"> <item internal="item 1 " pr_family="1" pr1="value 1" pr2="value 2"/> <item internal="item 1 " pr_family="2" pr1="value 1" pr2="value 2"/> <item internal="item 2 " pr_family="1" pr1="value 3" pr2="value 3"/> <item internal="item 2 " pr_family="2" pr1="value 3" pr2="value 3"/> <item internal="item 3 " pr_family="1" pr1="value 5" pr2="value 4"/> <item internal="item 3 " pr_family="2" pr1="value 5" pr2="value 4"/> </masterdata>

Of course, none of this answers the OP’s question; but it may provide a base upon which an answer can be constructed.

Athanasius <°(((><contra mundum

Replies are listed 'Best First'.
Re^2: modify perl 'excel to xml'
by Anonymous Monk on Jul 26, 2012 at 06:34 UTC
    Thankyou very much for your help. I'am novice in perl and I try to modify existing one. the script is for windows. in poor words:
    for example: how perl work now: read from these example excel ___________________________________ ---------|pr_family|___pr1___|___pr2___| ___________________________________ item 1 |___1,2__|__value 1_|_value 2_| item 2 |___1,2__|__value 3_|_value 3_| item 3 |___1,2__|__value 5_|_value 4_| convert to XML (output): item 1 - pr_family: 1 - pr_1: value 1 - pr_2: value 2 - item 1 - pr_family: 2 - pr_1: value 1 - pr_2: value 2 - item 2 - pr_family: 1 - pr_1: value 3 - pr_2: value 3 - item 2 - pr_family: 2 - pr_1: value 3 - pr_2: value 3 - item 3 - pr_family: 1 - pr_1: value 5 - pr_2: value 4 - item 3 - pr_family: 2 - pr_1: value 5 - pr_2: value 4 - _________________________________________________________ ok, but I need to modify theese perl because I must add moore than one + value in pr conlum, like is for family, with split (',') in pr1,pr2: example what the perl have to do after modify: excel ________________________________________________ ---------|_pr_family_|____pr1________|____pr2________| ________________________________________________ item 1 |___1,2____|_value 1,value 5_|____value 2_____| item 2 |___1,2____|_____value 3____|_value 3,value 4_| item 3 |___1,2____|_____value 5____|____value 4_____| convert to XML (output): item 1 - pr_family: 1 - pr_1: value 1 - pr_2: value 2 - item 1 - pr_family: 2 - pr_1: value 1 - pr_2: value 2 - item 1 - pr_family: 1 - pr_1: value 5 - pr_2: value 2 - item 1 - pr_family: 2 - pr_1: value 5 - pr_2: value 2 - item 2 - pr_family: 1 - pr_1: value 3 - pr_2: value 3 - item 2 - pr_family: 2 - pr_1: value 3 - pr_2: value 3 - item 2 - pr_family: 1 - pr_1: value 3 - pr_2: value 4 - item 2 - pr_family: 2 - pr_1: value 3 - pr_2: value 4 - item 3 - pr_family: 1 - pr_1: value 5 - pr_2: value 4 - item 3 - pr_family: 2 - pr_1: value 5 - pr_2: value 4 -