G'day documents9900,

This is fairly straightforward in XML::Simple; however, be aware that for more complex work, this is often not the best choice.

#!/usr/bin/env perl use strict; use warnings; use Inline::Files; use XML::Simple qw{:strict}; my %xml_hash = (Data => {}); my $xml_data = $xml_hash{Data}; my (%db1, %db2); while (<DB1>) { my ($root, $table, $key) = split; push @{$db1{$root}{$table}}, $key; } while (<DB2>) { my ($root, $table, $key) = split; push @{$db2{$root}{$table}}, $key; } while (<DIFF>) { my ($root, $table, $key, $col, $old, $new) = split; $xml_data->{$root}{$table}{NEW1}{KEY} = $db1{$root}{$table} if exists $db1{$root}{$table}; $xml_data->{$root}{$table}{NEW2}{KEY} = $db2{$root}{$table} if exists $db2{$root}{$table}; $xml_data->{$root}{$table}{MODIFIED}{KEY}{$key}{$col}{oldvalue} = +[$old]; $xml_data->{$root}{$table}{MODIFIED}{KEY}{$key}{$col}{newvalue} = +[$new]; } print XMLout(\%xml_hash, KeepRoot => 1, KeyAttr => {KEY => 'name'}); __DIFF__ Root1 TBLA KEY1 COLA A B Root1 TBLA KEY1 COLB D E Root1 TBLA KEY3 COLX M N Root2 TBLB KEY4 COLX M N Root2 TBLB KEY4 COLD A B Root3 TBLC KEY5 COLD A B __DB1__ Root1 TBLA KEY6 Root2 TBLB KEY7 Root3 TBLC KEY8 __DB2__ Root1 TBLA KEY9 Root1 TBLA KEY10 Root3 TBLC KEY11

Output:

$ pm_xml_db_diff.pl <Data> <Root1> <TBLA> <MODIFIED> <KEY name="KEY1"> <COLA> <newvalue>B</newvalue> <oldvalue>A</oldvalue> </COLA> <COLB> <newvalue>E</newvalue> <oldvalue>D</oldvalue> </COLB> </KEY> <KEY name="KEY3"> <COLX> <newvalue>N</newvalue> <oldvalue>M</oldvalue> </COLX> </KEY> </MODIFIED> <NEW1> <KEY>KEY6</KEY> </NEW1> <NEW2> <KEY>KEY9</KEY> <KEY>KEY10</KEY> </NEW2> </TBLA> </Root1> <Root2> <TBLB> <MODIFIED> <KEY name="KEY4"> <COLD> <newvalue>B</newvalue> <oldvalue>A</oldvalue> </COLD> <COLX> <newvalue>N</newvalue> <oldvalue>M</oldvalue> </COLX> </KEY> </MODIFIED> <NEW1> <KEY>KEY7</KEY> </NEW1> </TBLB> </Root2> <Root3> <TBLC> <MODIFIED> <KEY name="KEY5"> <COLD> <newvalue>B</newvalue> <oldvalue>A</oldvalue> </COLD> </KEY> </MODIFIED> <NEW1> <KEY>KEY8</KEY> </NEW1> <NEW2> <KEY>KEY11</KEY> </NEW2> </TBLC> </Root3> </Data>

-- Ken


In reply to Re: XML File Creation in Perl by kcott
in thread XML File Creation in Perl by documents9900

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.