in reply to Challenge: Parse XML Feed for Youtube channel
#!/usr/bin/perl use warnings; use strict; use XML::LibXML; use Template; my $xpc = 'XML::LibXML::XPathContext'->new; $xpc->registerNs(a => 'http://www.w3.org/2005/Atom'); $xpc->registerNs(y => 'http://www.youtube.com/xml/schemas/2015'); $xpc->registerNs(m => 'http://search.yahoo.com/mrss/'); my $dom = 'XML::LibXML'->load_xml(location => 'videos.xml'); my $title = $xpc->findvalue('/a:feed/a:title', $dom); my $name = $xpc->findvalue('/a:feed/a:author/a:name', $dom); my $entries; for my $entry ($xpc->findnodes('/a:feed/a:entry', $dom)) { my $title = $xpc->findvalue('a:title', $entry); my $url = $xpc->findvalue('a:link/@href', $entry); my $description = $xpc->findvalue('m:group/m:description', $entry) +; push @$entries, {url => $url, title => $title, description => $description || '--'}; } binmode *DATA, ':encoding(UTF-8)'; my $template = do { local $/; <DATA> }; my $tt = 'Template'->new; binmode *STDOUT, ':encoding(UTF-8)'; $tt->process(\$template, {title => $title, name => $name, entries => $entries}); __DATA__ <h3>[% title %]</h3> <b>[% name %]</b> <ul> [% FOR entry IN entries %] <li> [[% entry.url %]|[% entry.title | html %]] <p><i>[% entry.description | html %] </i></p></li> [% END %] </ul>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Challenge: Parse XML Feed for Youtube channel -- XML::LibXML + Template
by LanX (Saint) on Jun 28, 2022 at 21:47 UTC |