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


In reply to Re: modify perl 'excel to xml' by Athanasius
in thread modify perl 'excel to xml' by cibien

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.