ste*_*hom 2 perl string-interpolation xml-twig
在Perl中,我正在使用XML::Twig读取XML文件.某些属性的文本如下所示:
<p>Here is some text.</p>

<p>Some more text.
Run Code Online (Sandbox Code Playgroud)
我正在将此属性读入名为的变量中$Body.我想将这个变量打印到文件而不插入字符串中的特殊字符,即输出应该看起来与输入完全一样.我的代码看起来像:
open (my $OUT, ">", "out.csv") or die $!;
print $OUT $Body;
Run Code Online (Sandbox Code Playgroud)
但是,当我查看时out.csv,我看到:
<p>Here is some text.</p>
<p>Some more text.
Run Code Online (Sandbox Code Playgroud)
相反,我想看看原始字符串:
<p>Here is some text.</p>
&;#xA;<p>Some more text.
Run Code Online (Sandbox Code Playgroud)
我试过以下但没有成功:
print $OUT '$Body'; 不起作用,只显示"$ Body"print $OUT "$Body"; 不起作用,与没有引号相同.print $OUT qw{$Body}; 不起作用,只显示"$ Body".
这是一个完整的例子:
tmp.xml
<?xml version="1.0" encoding="utf-8"?>
<root>
<node Body="<p>Here is some text.</p>

<p>Some more text."/>
</root>
Run Code Online (Sandbox Code Playgroud)
码:
#!/usr/bin/perl
use strict;
use XML::Twig;
my $t=XML::Twig->new();
$t->parsefile("tmp.xml");
my $root= $t->root;
open (my $OUT, ">", "out.csv") or die();
my @nodes = $root->children('node'); # get the para children
foreach my $node (@nodes){
my $Body = $node->{'att'}->{'Body'};
print $OUT $Body;
}
Run Code Online (Sandbox Code Playgroud)
结果:
[dev@mogli:/swta] $ ./script.pl
[dev@mogli:/swta] $ cat out.csv
<p>Here is some text.</p>
<p>Some more text.
Run Code Online (Sandbox Code Playgroud)
XML :: Twig正在进行解码.传递keep_encoding旗帜以防止这种情况:
my $t = XML::Twig->new(keep_encoding => 1);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
312 次 |
| 最近记录: |