Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Challenge: Parse XML Feed for Youtube channel -- XML::LibXML + Template

by choroba (Cardinal)
on Jun 28, 2022 at 18:27 UTC ( [id://11145158]=note: print w/replies, xml ) Need Help??


in reply to Challenge: Parse XML Feed for Youtube channel

Using XML::LibXML and Template:
#!/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>

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

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
    Thanks.

    When I said "template" I meant anything maintainable with logical blocks of HTML. This includes here-docs when well organized and doesn't necessarily involve a template system or even MVC pattern.

    Or negatively expressed: I really despise° print statements with single tags.

    Not sure how to say that without the word "template". Suggestions welcome.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

    °) wrong word, it makes me cringe.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11145158]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-03-29 10:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found