XML::T...atterns(3pm)
π 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";
}
π COPYRIGHT π
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
- debian bookworm: libxml-treepuller-perl 0.1.2-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libxml-treepuller-perl 0.1.2-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libxml-treepuller-perl 0.1.2-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libxml-treepuller-perl 0.1.2-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libxml-treepuller-perl 0.1.2-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms