如何使用perl修改应反映在同一xml文件中的xml值? [英] How to modify the xml value that should reflect in the same xml file using perl?

查看:117
本文介绍了如何使用perl修改应反映在同一xml文件中的xml值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我编写的Perl脚本,用于修改xml文件中的特定值—. neo-datasource.xml.

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

我可以使用->toString在输出控制台中打印修改后的xml内容,但是我希望这些更改反映在称为neo-datasource.xml的同一个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.

您能否分享您的想法?

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;

推荐答案

您已经很近了—您只需要打开文件进行写入和打印即可(而不是STDOUT),例如:

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

这篇关于如何使用perl修改应反映在同一xml文件中的xml值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆