Below is a Perl script to create xml messages based on a template. It takes two files as input: an xml template and a list of values to put into the template. The values file consist of any number of lines with the following format: message number;element name; value. So you can choose how many xml messages you want to create and how many elements you want to fill in per message.

The script as such works, but this is one of my first actual Perl scripts. So any advice on what I could have done better is more than welcome. For instance, the way I use $lastreqnr as the upper limit of the for loop, feels more like a hack than a decent solution.

use warnings; use strict; use Data::Dumper; #configuration - to make using different templates etc. more easy my $xmltpl = "xmltpl.xml"; # xml template my $changes = "values.txt"; # values to be filled in my $reqname = "LoB_GPDLI"; # common part of request names #to avoid error message because of use strict my $slncnt=0; my %sfile = (); my $num=0; my $lastreqnr=0; my $i=0; my $req=(); # open file with the values and put into an hash of arrays open (my $fh1, "$changes"); my @arfile = <$fh1>; foreach my $arline (@arfile) { $slncnt++; my $sname = "sline" . "$slncnt"; chomp($arline); my @sline = split (/;/, $arline); $sfile{$sname} = [ @sline ]; $lastreqnr = $sfile{$sname}[0]; } # to check if the values file values has been read properly print "Total number of lines of changes is $slncnt\n"; print "Last req number is $lastreqnr\n"; print "\n"; # read the xml template, create a request file # and put in all the changes from the value file. for($i=1; $i <= $lastreqnr; $i++) { $num = 0; my $reqfile = "$reqname" . "_" . "$i" . ".xml"; print "reqname is $reqfile\n"; if (-e "$reqfile") { die "$reqfile already exists."; } open (my $req, ">>$reqfile") or die "Could not create req file"; open (my $tpl, "$xmltpl") or die "Could not open file"; while (<$tpl>) { my $tplln = $_; chomp $tplln; foreach my $group (keys %sfile) { if ($sfile{$group}[0] == $i) { $tplln =~ s{$sfile{$group}[1]}{$sfile{$group}[2]}; } } print $req $tplln; } close ($req) or die "Could not close request"; close ($tpl) or die "Could not close template"; print "\n"; } close ($fh1) or die "Could not close fh1";

In reply to Code to create set of xml messages based on template (review please) by j19sch

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.