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

南楼画角 提交于 2019-12-13 08:57:42

问题


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:


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);


来源:https://stackoverflow.com/questions/30912884/how-to-modify-the-xml-value-that-should-reflect-in-the-same-xml-file-using-perl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!