in reply to Re^3: XML File Creation in Perl
in thread XML File Creation in Perl

I did some correction in the code
while (<DB1>) { chomp; my ($root, $table, $header,$key) = split /\t/; $xml_data->{$root}{$table}{NEW1}{$key} = [$key]; } while (<DB2>) { chomp; my ($root, $table, $header,$key) = split /\t/; $xml_data->{$root}{$table}{NEW2}{$key} = [$key]; } while (<DIFF>) { chomp; my ($root, $table, $key, $col, $old, $new) = split /\t/; $xml_data->{$root}{$table}{MODIFIED}{KEY}{$key}{$col}{oldvalue} = [$ol +d]; $xml_data->{$root}{$table}{MODIFIED}{KEY}{$key}{$col}{newvalue} = [$ne +w]; }
Changes which i did

1) Removed the db1 and db2 code as it is not required.

2. Old code

$xml_data->{$root}{$table}{NEW2} = [$key];
New code
$xml_data->{$root}{$table}{NEW2}{$key} = [$key];
This was required as if there are multiple rows were there for a table which were overwritten with last value as the root, table combination already exists. So from the result set it displayed only the last row in the xml. Now the last problem is
$xml_data->{$root}{$table}{NEW2}{$key} = [$key];
the data is displayed as (Suppose Key value is ABC)
<ABC>ABC</ABC>
but it should be
<KEY>ABC</KEY>
This is coming because i added the key value for key only meaning code is working as expected, but i want KEY to be displayed instead of value

Replies are listed 'Best First'.
Re^5: XML File Creation in Perl
by kcott (Archbishop) on Apr 17, 2013 at 06:34 UTC

    Well, if

    $xml_data->{$root}{$table}{NEW2}{$key} = [$key];

    is producing

    <ABC>ABC</ABC>

    then I'd expect

    $xml_data->{$root}{$table}{NEW2}{KEY} = [$key];

    to produce

    <KEY>ABC</KEY>

    I recommend you spend some time playing around with the features of XML::Simple. Look at how the various OPTIONS interact with each other; see what differences running in STRICT MODE makes; and so on.

    -- Ken

      Thanks Ken. I will look in detailed documentation on this. Also, I agree with your point on and the same i also mentioned that it is expected. My point is If i have multiple combinations of root, table, NEW2 isn't that every time the loop runs it overwrites the old value because we are storing $key for  $xml_data->{$root}{$table}{NEW2}{KEY} So to avoid that, i used $xml_data->{$root}{$table}{NEW2}{$key} = [$key]; but it resulted into
      <ABC>ABC</ABC> <DEF>DEF</DEF>
      , without this it displays only the last row with that combination in the file <KEY>DEF</KEY>

        Instead of assigning:

        $xml_data->{$root}{$table}{NEW2}{KEY} = [$key]

        Try pushing:

        push @{$xml_data->{$root}{$table}{NEW2}{KEY}}, $key;

        There's a few examples of that in the code I posted earlier.

        -- Ken