输入文件包含多个换行符,空标记如下:
<html>
<body>
<title>XXX</title>
<p>text...</p>
<collaboration seq="">
<ce:text></ce:text>
</collaboration>
...
<p>text</p>
<collaboration seq="">
<ce:text>AAA</ce:text>
</collaboration>
<p>text</p>
</body>
</html>
只需一个换行符所需的输出文件,必须删除空标记
<html>
<body>
<title>XXX</title>
<p>text...</p>
...
<p>text</p>
<p>text</p>
<collaboration seq="">
<ce:text>AAA</ce:text>
</collaboration>
</body>
</html>
已经尝试过的编码:
print "Enter the file name without extension: ";
chomp($filename=<STDIN>);
open(RED,"$filename.txt") || die "Could not open TXT file";
open(WRIT,">$filename.html");
while(<RED>)
{
#process in file
s/<collaboration seq="">\n<ce:text><\/ce:text>\n<\/collaboration>//g;
s/\n\n//g;
print WRIT $_;
}
close(RED);
close(WRIT);
以上编码不会清除所需的任何内容......如何解决?
答案 0 :(得分:0)
首先你应该实际上啜饮文件。所以我们假设您使用zigdon's method:
my $file;
{
print "Enter the file name without extension: ";
my $filename = <STDIN>
chomp($filename);
open F, $filename or die "Can't read $filename: $!";
local $/; # enable slurp mode, locally.
$file = <F>;
close F;
}
现在$file
包含文件的内容,因此您可以使用它。
#process in file
$file ~= s/<collaboration seq="">\R<ce:text><\/ce:text>\R<\/collaboration>//g;
$file ~= s/\R{2,}/\n/g; #I'm guessing this is probably what you intended
print WRIT $file;
答案 1 :(得分:0)
您可以使用XML :: Simple:
# use XML simple to process the XML
my $xs = XML::Simple->new(
# remove extra whitespace
NormaliseSpace => 2,
# keep root element
KeepRoot => 1,
# force elements to arrays
ForceArray => 1,
# ignore empty elements
SuppressEmpty => 1
);
# read in the XML
my $ref = $xs->XMLin($xml);
# print out the XML minus the empty tags
print $xs->XMLout($ref);