XML::T...atterns(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::Patterns β€” recipes for dealing with XML patterns, bestie πŸ’–

okay so this is a cookbook page from the User Contributed Perl Documentation for XML::TreePuller, and it's literally just a lil collection of copy-paste-able snippets for the two most common XML shapes you'll ever meet: lists and hashes. no cap. every example is a full runnable script, so you can steal them and go. 🫢


✨ LISTS ✨

first up: you got a bunch of <element>s hanging out inside an <array> and you want them all in a Perl array. the move is $root->get_elements('element') and then map over it pushing $_->text into @array. that's it, that's the whole vibe. 🌸

#!/usr/bin/env perl

use strict;
use warnings;
use Data::Dumper;

use XML::TreePuller;

my $xml = <<EOF;
<array>
      <element>zero</element>
      <element>one</element>
      <element>two</element>
</array>

EOF

my $root = XML::TreePuller->parse(string => $xml);
my @array;

map({ push(@array, $_->text) } $root->get_elements('element'));

print join(' ', @array), "\n";

πŸ¦‹ HASHES πŸ¦‹

now the same energy but for key/value situations. there's two flavours and honestly both eat. πŸ’…

πŸ’– With attributes

here the key lives in an attribute('key') on each <entry>, and the value is the element's text. you map over the elements and assign straight into %hash. iconic.

#!/usr/bin/env perl

use strict;
use warnings;

use XML::TreePuller;

my $xml = <<EOF;
<hash>
      <entry key="0">zero</entry>
      <entry key="1">one</entry>
      <entry key="2">two</entry>
</hash>

EOF

my $root = XML::TreePuller->parse(string => $xml);
my %hash;

map({ $hash{$_->attribute('key')} = $_->text; } $root->get_elements('entry'));

foreach (0..2) {
      print $hash{$_}, "\n";
}

πŸŽ€ Without attributes

no attributes? no problem. the keys and values are just their own elements, so you grab @keys with get_elements('key/') and @values with get_elements('value/'), then shift(@values) your way through them inside the map. the way that trailing slash in the path does work here... obsessed. 😭

#!/usr/bin/env perl

use strict;
use warnings;
use Data::Dumper;

use XML::TreePuller;

my $xml = <<EOF;
<hash>
  <key>0</key>
  <value>zero</value>
  <key>1</key>
  <value>one</value>
  <key>2</key>
  <value>two</value>
</hash>

EOF

my $root = XML::TreePuller->parse(string => $xml);
my %hash;

my @keys = $root->get_elements('key/');
my @values = $root->get_elements('value/');

map({ $hash{$_->text} = shift(@values)->text } @keys);

foreach (0..2) {
      print $hash{$_}, "\n";
}

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

so yeah, credit where credit is due, go read the README for the licensing deets before you run off with these snippets. πŸ’œ


aaaand that's the whole page bestie, ily, go parse something pretty 🌈✨

perl v5.34.0 Β· 2022-06-28 β€” that's the version and the date this page shipped, mwah πŸ’…πŸŽ€

πŸ“¦ where this page comes from