payam77 has asked for the wisdom of the Perl Monks concerning the following question:

Hi there, I am fairly new to Perl and I've been assigned to do something very complex in a short period of time. A small part of my job is to write a Perl script that automatically adds a new release tab to the Hudson Build server each time we branch to a new release. Hudson uses the following config xml file and generates a GUI with fancy View tabs that list all our release projects. Can some one please help me write a Perl script that is able to add xml code to the following xml file by feeding it just the new release name(Tab Names in the xml file), and the script adds similar Tabs to Hudson that will contain all projects (zip and zip1) listed under the Tab 1, 2 and 3 with new names accordingly. (ex: Tab 4, 5 and 6). In addition, I also need to be able to remove these View Tabs when necessary. If anyone can help me get started on this, it will be much appreciated. Do I use "use XML::DOM;"? Thanks, Tony.
<views> <listView> <owner reference="../../.."/> <jobNames class="tree-set"> <comparator class="hudson.util.CaseInsensitiveComparator"/> <string>zip</string> </jobNames> <name>Tab1</name> </listView> <listView> <owner reference="../../.."/> <jobNames class="tree-set"> <comparator class="hudson.util.CaseInsensitiveComparator" refe +rence="../../../listView/jobNames/comparator"/> <string>zip1</string> </jobNames> <name>Tab2</name> </listView> <listView> <owner reference="../../.."/> <jobNames class="tree-set"> <comparator class="hudson.util.CaseInsensitiveComparator" refe +rence="../../../listView/jobNames/comparator"/> <string>zip</string> <string>zip1</string> </jobNames> <name>Tab3</name> </listView> </views> <slaveAgentPort>0</slaveAgentPort> <secretKey>6afc684a9a6f353335bd0f68beccc999f3adc88cac</secretKey> </hudson>

Replies are listed 'Best First'.
Re: Removing and Appending to an XML file
by Joost (Canon) on Jul 03, 2008 at 00:13 UTC
Re: Removing and Appending to an XML file
by mirod (Canon) on Jul 03, 2008 at 12:57 UTC

    I can't really read your question (did you preview it?) but the answer ;--) is most likely not to use XML::DOM. A short answer would be to use XML::Simple (if appropriate, if you don't get it to work easily, try an other module), XML::LibXML or XML::Twig. See the Perl 5 wiki for a little more details.

      Thanks for your help.
Re: Removing and Appending to an XML file
by Anonymous Monk on Jul 03, 2008 at 21:10 UTC
Re: Removing and Appending to an XML file
by Jenda (Abbot) on Jul 07, 2008 at 23:35 UTC

    I'm not sure I understand you right, but if you wanted to be able to remove or copy a <listView> (including contents) based on the value of the <name> tag you might do something like this:

    use strict; use warnings; no warnings 'uninitialized'; use Storable qw(dclone); use XML::Rules; my $parser =XML::Rules->new( style => 'filter', start_rules => { views => sub { if (exists $_[4]->{parameters}{remove}) { if (ref($_[4]->{parameters}{remove}) eq 'ARRAY') { my %tmp; @tmp{@{$_[4]->{parameters}{remove}}} = (); $_[4]->{parameters}{remove} = \%tmp; } elsif (ref($_[4]->{parameters}{remove}) ne 'HASH') +{ die "The remove parameter must be either a HASH or + ARRAY reference!\n"; }; } if (exists $_[4]->{parameters}{copy} and ref($_[4]->{param +eters}{copy}) ne 'HASH') { die "The copy parameter must be either a HASH or ARRAY + reference!\n"; } 1; } }, rules => { _default => 'raw extended', listView => sub { my $name = $_[1]->{':name'}{_content}; if (exists $_[4]->{parameters}{copy}{$name}) { if (exists $_[4]->{parameters}{remove}{$name}) { # rename $_[1]->{':name'}{_content} = $_[4]->{parameters}{c +opy}{$name}; return $_[0] => $_[1]; } else { # copy my $copy = dclone($_[1]); $copy->{':name'}{_content} = $_[4]->{parameters}{c +opy}{$name}; # return $_[0] => $_[1], $_[0] => $copy; return [ [$_[0] => $_[1]], "\n ", [$_[0] => $co +py]]; } } elsif (exists $_[4]->{parameters}{remove}{$name}) { # remove return; } else { # nothing return $_[0] => $_[1]; } }, } ); $parser->filter(\*DATA, \*STDOUT, { # remove => [qw(Tab1)], copy => {Tab1 => 'NeTab'} }); __DATA__ <hudson> <views> <listView> <owner reference="../../.."/> <jobNames class="tree-set"> <comparator class="hudson.util.CaseInsensitiveComparator"/> <string>zip</string> </jobNames> <name>Tab1</name> </listView> <listView> <owner reference="../../.."/> <jobNames class="tree-set"> <comparator class="hudson.util.CaseInsensitiveComparator" refe +rence="../../../listView/jobNames/comparator"/> <string>zip1</string> </jobNames> <name>Tab2</name> </listView> <listView> <owner reference="../../.."/> <jobNames class="tree-set"> <comparator class="hudson.util.CaseInsensitiveComparator" refe +rence="../../../listView/jobNames/comparator"/> <string>zip</string> <string>zip1</string> </jobNames> <name>Tab3</name> </listView> </views> <slaveAgentPort>0</slaveAgentPort> <secretKey>6afc684a9a6f353335bd0f68beccc999f3adc88cac</secretKey> </hudson>