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.


In reply to Re: XML File Creation in Perl by hdb
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.