XML::T...:Intro(3pm)

πŸ“¦
version 0.1.2 Β· 2 versions
  • 0.1.2 Β· debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking (latest)
  • 0.1.2 Β· ubuntu jammy
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

✨ NAME ✨

XML::TreePuller::CookBook::Intro β€” bestie, this is the intro chapter of the XML::TreePuller cookbook, and it's literally just a bunch of ways to work with an Atom feed πŸ’–


🌸 ABOUT 🌸

Okay so first things first: Atom documents are small and simple. They fit all cozy inside RAM, and they don't have a million nested elements going on. Processing them is super straight forward, which is exactly why it's the perfect little starter quest for learning how this all works. No cap. πŸ¦‹

πŸŽ€ Atom Format πŸŽ€

An Atom feed looks, like, exactly like this:

<?xml version="1.0" encoding="utf-8"?>

<feed xmlns="http://www.w3.org/2005/Atom">

      <title>Example Feed</title>
      <subtitle>A subtitle.</subtitle>
      <link href="http://example.org/feed/" rel="self" />
      <link href="http://example.org/" />
      <id>urn:uuid:60a76c80-d399-11d9-b91C-0003939e0af6</id>
      <updated>2003-12-13T18:30:02Z</updated>
      <author>
              <name>John Doe</name>
              <email>johndoe@example.com</email>
      </author>

      <entry>
              <title>Atom-Powered Robots Run Amok</title>
              <link href="http://example.org/2003/12/13/atom03" />
              <link rel="alternate" type="text/html" href="http://example.org/2003/12/13/atom03.html"/>
              <link rel="edit" href="http://example.org/2003/12/13/atom03/edit"/>
              <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
              <updated>2003-12-13T18:30:02Z</updated>
              <summary>Some text.</summary>
      </entry>

</feed>

πŸ‘‘ PROGRAMS πŸ‘‘

πŸ“ Feed summaries

So imagine you've got 10 Atom feeds you're lowkey obsessed with and you wanna subscribe, but first you need a little peek at what each one is serving. Perl to the rescue, obviously πŸ’… The script below builds a report of however many Atom feeds you want, off the Internet, pulling them straight from a URL or from a file. The report comes out looking like this:

Feed: Example Feed
  * Atom-Powered Robots Run Amok

(and honestly? that sure does sound like an interesting feed πŸ”₯)

#!/usr/bin/env perl

use strict;
use warnings;

use XML::TreePuller;

foreach (@ARGV) {
      my $root = XML::TreePuller->parse(location => $_);
      my $title = $root->xpath('/feed/title')->text;

      print "Feed: $title\n";

      foreach ($root->xpath('/feed/entry/title')) {
              print "  * ", $_->text, "\n";
      }

      print "\n\n\n";
}

🌈 Linking to entries

Okay so you've got an Atom feed, and the question is: what's the easiest way to build an HTML list of hyperlinks to all the entries in it? 🧚 You need two things β€” the title, which lives all by itself in a single element, and the hyperlink to the entry. Now, there are multiple link elements, and we only want one of them: the one whose "rel" attribute value is "alternate". XPath makes quick work of this, periodt.

#!/usr/bin/env perl

use strict;
use warnings;

use XML::TreePuller;

my $root = XML::TreePuller->parse(location => shift(@ARGV));

print "<ul>\n";

foreach($root->xpath('/feed/entry')) {
      my $title = $_->xpath('//title')->text;
      #there are many link elements but we only want one of them
      my $to = $_->xpath("//link[\@rel='alternate']")->attribute('href');

      print "  <li><a href=\"$to\">$title</a></li>\n";
}

print "</ul>\n";

The ATOM example XML document was taken from Wikipedia at the following URL:

http://en.wikipedia.org/w/index.php?title=Atom_(standard)&oldid=353180236

and it's available under the Creative Commons Attribution ShareAlike license 🫢

All other content is copyright Tyler Riddle; see the README for licensing terms.


and that's the whole intro, bestie!! this was perl v5.34.0, dated 2022-06-28, and it lives in XML::T...:Intro(3pm) β€” go forth and pull some trees πŸ’…βœ¨

πŸ“¦ where this page comes from