1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
use strict;
use warnings;
use Data::Dumper;
use Benchmark qw(:all);
use FileHandle;
use XML::LibXML;
use XML::RSS;
use XML::Simple;
$XML::Simple::PREFERRED_PARSER = 'XML::Parser';
use XML::RSS::LibXML;
use XML::RSS::Parser;
use XML::FeedPP;
use XML::RSSLite;
my $rss_file = "../satomi.xml";
my $fh = FileHandle->new($rss_file)
or die "cannot open $rss_file: $!";
local $/; # slurp mode
our $content = $fh->getline;
$fh->close;
cmpthese(timethese(0,
{
'regexp' => \&with_regexp,
'XML::Simple' => \&with_xml_simple,
'XML::RSS' => \&with_xml_rss,
'XML::LibXML' => \&with_xml_libxml,
'XML::RSS::LibXML' => \&with_xml_rss_libxml,
'XML::RSS::Parser' => \&with_xml_rss_parser,
'XML::FeedPP' => \&with_xml_feedpp,
'XML::RSSLite' => \&with_xml_rsslite,
}));
sub with_xml_rsslite {
my @links = ();
my %result;
parseRSS(\%result, \$content);
for my $item (@{$result{item}}) {
push @links, $item->{link};
}
# print Dumper \@links;
}
sub with_xml_feedpp {
my @links = ();
my $feed = XML::FeedPP->new($content);
foreach my $item ( $feed->get_item() ) {
push @links, $item->link;
}
# print Dumper \@links;
}
sub with_xml_rss_parser {
my @links = ();
my $parser = XML::RSS::Parser->new;
my $feed = $parser->parse_string($content);
foreach my $item ( $feed->query('item') ) {
push @links, $item->query('link')->text_content;
}
# print Dumper \@links;
}
sub with_regexp {
my $pattern = "<item .*?>.*?<link>(.*?)</link>.*?</item>";
my @links =
($content =~ m/$pattern/smg);
# print Dumper \@links;
}
sub with_xml_simple {
my @links = ();
my $parser = XML::Simple->new;
my $data = $parser->XMLin($content, ForceArray => 1);
for my $item (@{$data->{item}}) {
push @links, $item->{link}[0];
}
# print Dumper \@links;
}
sub with_xml_rss {
my @links = ();
my $rss = XML::RSS->new;
$rss->parse($content);
for my $item (@{$rss->{items}}) {
push @links, $item->{link};
}
# print Dumper \@links;
}
sub with_xml_libxml {
my @links =();
my $parser = XML::LibXML->new;
my $doc = $parser->parse_string($content);
my @nodes = $doc->findnodes(
"//*[local-name()='item']/*[local-name()='link']/text()"
);
for my $node (@nodes) {
push @links, $node->nodeValue;
}
# print Dumper \@links;
}
sub with_xml_rss_libxml {
my @links = ();
my $rss = XML::RSS::LibXML->new;
$rss->parse($content);
for my $item (@{$rss->{items}}) {
push @links, $item->{link};
}
# print Dumper \@links;
}
|