在perl中,我希望将现有xml文件中的标记内容(firstclip)更改为secondclip
虽然你确定apkName实际上是这样的吗?“关闭”标签会出现在它所在的位置,这似乎很奇怪 重写现有文件-在perl中,我希望将现有xml文件中的标记内容(firstclip)更改为secondclip,perl,Perl,虽然你确定apkName实际上是这样的吗?“关闭”标签会出现在它所在的位置,这似乎很奇怪 重写现有文件-XML::Twig有一个parsefile\u-in-place机制,但我建议它对于您尝试执行的操作来说过于复杂,而您只想 #!/usr/bin/env perl use strict; use warnings; use XML::Twig; #parse your XML. my $twig = XML::Twig -> new -> parsefile ( 'your_f
XML::Twig
有一个parsefile\u-in-place
机制,但我建议它对于您尝试执行的操作来说过于复杂,而您只想
#!/usr/bin/env perl
use strict;
use warnings;
use XML::Twig;
#parse your XML.
my $twig = XML::Twig -> new -> parsefile ( 'your_file.xml' );
#search for, and modify 'clipName' nodes containing the text 'firstclip'.
$_ -> set_text('secondclip') for $twig -> findnodes('//clipName[string()="firstclip"]');
$twig -> set_pretty_print('indented');
$twig -> print;
Ya它不会写入现有的xml它只会打印我想在修改后写入现有xml的数据要打开文件句柄,请将
$twig->sprint
写入其中。这是最简单的部分。如果我想在没有解析文本的情况下更改clipname标记内容,就像我想传递一些内容一样?对不起,我不明白这个问题。
use XML::Simple;
my $xml_file = "test.xml";
my $xml = XMLin(
$xml_file,
KeepRoot => 1,
ForceArray => 1,
)
$xml->{root}->[0]->{Arguments}->[0]->{apkName}->[0]->{clipName}->[0] = 'secondclip';
XMLout(
$xml,
XMLDecl =>1,
KeepRoot => 1 ,
NoAttr => 1,
OutputFile => $xml_file,
);
#!/usr/bin/env perl
use strict;
use warnings;
use XML::Twig;
#parse your XML.
my $twig = XML::Twig -> new -> parsefile ( 'your_file.xml' );
#search for, and modify 'clipName' nodes containing the text 'firstclip'.
$_ -> set_text('secondclip') for $twig -> findnodes('//clipName[string()="firstclip"]');
$twig -> set_pretty_print('indented');
$twig -> print;
open ( my $output, '>', 'output.new.xml' ) or die $!;
print {$output} $twig -> sprint;