看起来当您映射到文本时,您可以替换XML实体,但是当您使用节点并使用其内容时,将保留实体。这个最小的例子:
#!/usr/bin/perl
use strict;
use warnings;
use Mojo::DOM;
my $dom = Mojo::DOM->new(‘
this & "that"
‘);
for my $phrase ($dom->find(‘p’)->each) {
print $phrase->content(), “\n”;
}
</code>
打印:
this & "that"
</code>
如果你想保持循环和地图,请替换
map(‘text’)
同
map(‘content’)
像这样:
for my $phrase ($dom->find(‘p’)->map(‘content’)->each) {
</code>
如果您有嵌套标签并且只想查找文本(但不打印那些嵌套标签名称,只打印它们的内容),则需要扫描DOM树:
#!/usr/bin/perl
use strict;
use warnings;
use Mojo::DOM;
my $dom = Mojo::DOM->new(‘
this & "that"
done
‘);
for my $node (@{$dom->find(‘p’)->to_array}) {
print_content($node);
}
sub printcontent {
my ($node) = @;
if ($node->type eq “text”) {
print $node->content(), “\n”;
}
if ($node->type eq “tag”) {
for my $child ($node->child_nodes->each) {
print_content($child);
}
}
}
</code>
打印:
this &
“
that
“
done
</code>