【发布时间】:2015-08-01 22:41:20
【问题描述】:
我是 Perl 新手,我的任务是替换或删除多个 XML 文件中的注释部分 (<!--),替换后我需要将这些多个 XML 文件移动到其他目录。我在下面说明我的D:\folder1\ 有许多xml 文件a.xml、b.xml、c.xml 等等,替换后我需要将所有文件从folder1 移动到D:\folder2\。
我通过替换 - 和 @@@ 尝试了一个文件,但我不知道从目录中的 xml 文件中删除注释行 (<!--add aaa -->)。
下面是我的 Perl 代码
#!/usr/bin/perl
use strict;
use warnings;
my $tag = 'SHORT_DESC';
open my $input_file, '<', 'test.xml' or die $!;
open my $output_file, '>', 'test_out.xml' or die $!;
my $input;
{
local $/; #Set record separator to undefined.
$input = <$input_file>; #This allows the whole input file to be read at once.
}
$input =~ s/&/@@@/g;
$input =~ s/^- (?=<)//gm;
$input =~ s/<header[^>]*>\K\s*<header[^>]*>//gis;
close $input_file or die $!;
print {$output_file} $input;
close $output_file or die $!;
我的 XML 是
<?xml version = '1.0' encoding = 'UTF-8'?>
<!-- Order details-->
<order>
<Names>
<!-- Names-->
<content>This is dummy content</content>
</Names>
</order>
【问题讨论】:
-
您是否因为特定原因而错误地输入了 XML cmets?为什么引用
@@@?另外,my $input = do { local $/; <$input_file> };。另外,您没有使用 UTF-8 层阅读。 -
这样的文件我没有输入
-
您是否声称文件中的 cmets 按字面意思使用
<\!--?在这种情况下,它们不是 XML。 -
思南我的要求是从文件中删除该注释行,它的纯xml文件,示例目的我更改了代码。