How to modify the xml value that should reflect in the same xml file using perl?

68 views Asked by At

Here is the perl script I have written to modify the particular value from the xml file — neo-datasource.xml.

I can print the modified xml contents in the output console using ->toString, but I wish to have these changes to be reflected in the same xml file called neo-datasource.xml instead of printing it in console.

Could you please share your ideas?

use strict;
use warnings;

use XML::Twig;


my $twig = XML::Twig->new( keep_spaces => 1 );

$twig->parsefile('C:\Users\IBM_ADMIN\Desktop\dbautomate\neo-datasource.xml');

my ($class_string) = $twig->findnodes('//var[@name="d1new1d1"]/struct[@type="coldfusion.server.ConfigMap"]/var[@name="password"]/string');
$class_string->set_text('NoDatabase');


print $twig->toString;
1

There are 1 answers

0
TobyLL On

You're very nearly there — you just need to open the file for writing and print to it (not to STDOUT), something like:

open(my $fh, '>', 'C:\Users\IBM_ADMIN\Desktop\dbautomate\neo-datasource .xml')
    or die "Cannot open XML file for writing\n";
$fh->print($twig->toString);