SEO

Post

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

Solution:

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

 

#   Usage: edit_xml.pl -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 )

{

   Usage();

   exit(0);

}

 

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

{

   Usage();

   exit(-1);

}

 

# 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";

close(CTXOUT);

 

exit(0);

 

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 edit_xml.pl Perl script:

 

unset PERL5LIB

 

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

 

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

 

Contacts

www.RadugaApps.com

Feel free to contact our representative at [email protected]

Comments

There are not comments

Leave a comment