in reply to XML File Creation in Perl

My proposal was: Read your desired XML with XML::Simple, use Data::Dumper to see what the resulting structure is, then create it from all your data, and use XMLout to write your xml file. Here is the script to do that.

But then I thought: Will this work? All this effort. Let's test first and look at what XMLout does to what XMLin has created. And see: the output is different.

use strict; use warnings; use Data::Dumper; use XML::Simple; my $xml = <<XML; <Data> <Root1> <TBLA> <NEW1> <KEY>KEY6</KEY> </NEW1> <NEW2> <KEY>KEY9</KEY> <KEY>KEY10</KEY> </NEW2> <MODIFIED> <KEY name ="KEY1"> <COLA> <oldvalue>A</oldvalue> <newvalue>B</newvalue> </COLA> <COLB> <oldvalue>D</oldvalue> <newvalue>E</newvalue> </COLB> </KEY> <KEY name ="KEY3"> <COLX> <oldvalue>M</oldvalue> <newvalue>N</newvalue> </COLX> </KEY> </MODIFIED> </TBLA> </Root1> </Data> XML my $ref = XMLin( $xml ); print Dumper($ref); print XMLout( $ref, RootName=>"Data" );

which creates

<Data> <Root1 name="TBLA"> <MODIFIED name="KEY"> <KEY1 name="COLA" newvalue="B" oldvalue="A" /> <KEY1 name="COLB" newvalue="E" oldvalue="D" /> <KEY3 name="COLX" newvalue="N" oldvalue="M" /> </MODIFIED> <NEW1 KEY="KEY6" /> <NEW2> <KEY>KEY9</KEY> <KEY>KEY10</KEY> </NEW2> </Root1> </Data>

The question now is: Is that ok for you? Or do you need more control, in which case there are a number of options in XML::Simple you can play with or you have to take another route.

PS: There were a number of inconsistencies in the sample xml you have provided.

Replies are listed 'Best First'.
Re^2: XML File Creation in Perl
by documents9900 (Initiate) on Apr 16, 2013 at 14:47 UTC
    Thanks for your help so far. Let me give you some actual data so that it will be easy to interpret.
    ROOT OBJECT KEY COLUMN OLD NEW EMPLOYEE EMPLOYEE XYZ TITLE <null> Mr EMPLOYEE EMPDETAILS DEF CITY California New York CUSTOMER CUSTOMER ABC CAPTION Regular Premium
    File 2
    EMPLOYEE EMPLOYEE NEW EMPLOYEE 1 EMPLOYEE EMPLOYEE NEW EMPLOYEE 9 EMPLOYEE EMPDETAILS NEW EMPLOYEE1-DETAILS EMPLOYEE EMPDETAILS NEW EMPLOYEE9-DETAILS EMPLOYEE EMPDETAILS NEW EMPLOYEE16-DETAILS
    File 3
    EMPLOYEE EMPLOYEE NEW EMPLOYEE 6 EMPLOYEE EMPDETAILS NEW EMPLOYEE6-DETAILS CUSTOMER CUSTOMER NEW CUSTOMER
    So from these three files i wrote small perl program to generate the files in xml
    <Data> <EMPLOYEE> <KEY name = 'XYZ'> <TITLE> <oldvalue></oldvalue> <newvalue>Mr</newvalue> </TITLE> </KEY> </EMPLOYEE> <EMPDETAILS> <KEY name = 'DEF'> <CITY> <oldvalue>California</oldvalue> <newvalue>New York</newvalue> </CITY> </KEY> </EMPDETAILS> <CUSTOMER> <KEY name = 'ABC'> <CAPTION> <oldvalue>Regular</oldvalue> <newvalue>Premium</newvalue> </CAPTION> </KEY> </CUSTOMER> </Data>
    Output 2
    <Data> <EMPLOYEE> <KEY>'NEW EMPLOYEE 1'</KEY> <KEY>'NEW EMPLOYEE 9'</KEY> </EMPLOYEE> <EMPDETAILS> <KEY>'NEW EMPLOYEE1-DETAILS'</KEY> <KEY>'NEW EMPLOYEE9-DETAILS'</KEY> <KEY>'NEW EMPLOYEE16-DETAILS'</KEY> </EMPDETAILS> </Data>
    Output 3
    <Data> <EMPLOYEE> <KEY>'NEW EMPLOYEE 6'</KEY> </EMPLOYEE> <EMPDETAILS> <KEY>'NEW EMPLOYEE6-DETAILS'</KEY> </EMPDETAILS> <CUSTOMER> <KEY>'NEW CUSTOMER'</KEY> </CUSTOMER> </Data>
    Now i need to combine all these three into single output file. First field tells the root object. So for EMPLOYEE, i need to display details of EMPLOYEE and EMPDETAILS table what has been in one db, other db, and if exists in both db what are the old and new values (these all are created from perl program and it contains only delta information). Similary for CUSTOMER root object, i need to display all entities of this object and then what exists in one db,other db, modified information. There are many occurances of modified columns i.e. for each key several columns can be modified

    There are no headers in any file. I have added one in first input for clarification