How to programmatically update the context file

Problem Definition:

The application context file sometimes needs to be updated during application configuration. It is possible to update it programmatically.

Environment Clone


Here is the Perl script that updates the value of the context file node:


#   Usage: -f file -n node -v value [-h]


#          -f file       Context file

#          -n node    Node (example: oa_context/oa_system/oa_disco_server/disco_ver_comma)

#          -v value   Text value (example: 10,1,2)

#          -h              Help


use strict;

use XML::XPath;

use XML::XPath::XMLParser;

use Getopt::Long;


my $ctxfile;      # Context file

my $node;         # Node to update

my $value = "";   # New value

my $help;

my $result = "";

my $curr_date = `date +%d%m%y_%H%M%S`;


# Read command line arguments

# ----------------------------

GetOptions("-f|file=s" => \$ctxfile,

                    "-n|node=s" => \$node,

                    "-v|value:s" => \$value,

                    "-h|help" => \$help);


if ( $help )






if (!defined($ctxfile) || !defined($node) || !defined($value))






# Backup the context file

# ------------------------------

my $backup = $ctxfile . "_" . $curr_date;

chomp $backup;

my @cmd = ("cp","$ctxfile","$backup");

system (@cmd) == 0 or die "Could not backup context file";


# Open the context file

# ------------------------------

my $xp = XML::XPath->new(filename => $ctxfile) or die "Could not open context file";

my($root) = $xp->findnodes('/') or die "Could not parse context file";


# Update the context file

# ------------------------------

$xp->setNodeText($node,$value) or die "Could not set node text";


$_ = $root->toString();

s/ \/>/\/>/g;


# Save the context file

# ------------------------------

open (CTXOUT,">$ctxfile") or die "Could not open context file for writing";


print CTXOUT "<?xml version = '1.0'?>\n";

print CTXOUT "$_\n";





sub Usage


   print "\n";

   print "   Usage: $0 -f file -n node -v value [-h]\n";

   print "\n";

   print "  -f file  Context file\n";

   print "  -n node  Node (example: oa_context/oa_system/oa_disco_server/disco_ver_comma)\n";

   print "  -v value Text value (example: 10,1,2)\n";

   print "  -h       Help\n";

   print "\n";


Here is the shell command that executes the Perl script:


unset PERL5LIB


/usr/bin/perl -I /usr/lib/perl5/vendor_perl/5.8.8/XML $*


# Note: /usr/lib/perl5/vendor_perl/5.8.8/XML is a directory that contains



There are not comments

Leave a comment