【发布时间】:2011-12-21 10:42:55
【问题描述】:
如何使用 Mojo::DOM 模块编写这个示例?
#!/usr/bin/env perl
use warnings;
use 5.012;
use XML::LibXML;
my $string =<<EOS;
<result>
<cd>
<artists>
<artist class="1">Pumkinsingers</artist>
<artist class="2">Max and Moritz</artist>
</artists>
<title>Hello, Hello</title>
</cd>
<cd>
<artists>
<artist class="3">Green Trees</artist>
<artist class="4">The Leons</artist>
</artists>
<title>The Shield</title>
</cd>
</result>
EOS
#/
my $parser = XML::LibXML->new();
my $doc = $parser->load_xml( string => $string );
my $root = $doc->documentElement;
my $xpath = '/result/cd[artists/artist[@class="2"]]/title';
my @nodes = $root->findnodes( $xpath );
for my $node ( @nodes ) {
say $node->textContent;
}
【问题讨论】:
标签: xml perl mojolicious