Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
174 views
in Technique[技术] by (71.8m points)

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

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:UsersIBM_ADMINDesktopdbautomate
eo-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;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:UsersIBM_ADMINDesktopdbautomate
eo-datasource .xml')
    or die "Cannot open XML file for writing
";
$fh->print($twig->toString);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...