XML::Pa...e::ETree(3)
π¦ XML::Parser::Style::ETree β the Perl doc page, but make it make sense π¦
okay so bestie, this is a page from the User Contributed Perl Documentation set β it's XML::Parser::Style::ETree, and it's basically the moment XML turns into a cute little Perl hash for you. no cap, it's giving "tree but make it data structure" π
β¨ NAME β¨
XML::Parser::Style::ETree β parse xml to simple tree. that's it, that's the whole vibe π
πΈ VERSION πΈ
Version 0.09 β yes queen, we're on 0.09 π
π§ SYNOPSIS π§
the whole setup is literally two lines:
use XML::Parser;
my $p = XML::Parser->new( Style => 'ETree' );
that's the entire ritual. slay β¨
π EXAMPLE π
okay so imagine this lil XML:
<root at="key">
<nest>
first
<v>a</v>
mid
<v at="a">b</v>
<vv></vv>
last
</nest>
</root>
it will be β and honestly this is iconic β
{
root => {
'-at' => 'key',
nest => {
'#text' => 'firstmidlast',
vv => '',
v => [
'a',
{
'-at' => 'a',
'#text' => 'b'
}
]
}
}
}
see how the text got all smooshed together into '#text' => 'firstmidlast'? that's not a bug bestie, that's the whole point π«Ά
π SPECIAL VARIABLES π
these are the knobs you can turn. global package variables, main character energy, handle with care π
π
$TEXT{ATTR} [ = '-' ]
lets you set the prefix for the name of attribute nodes, periodt.
<item attr="value" />
# will be
item => { -attr => 'value' };
# with
$TEXT{ATTR} = '+';
# will be
item => { '+attr' => 'value' };
lowkey obsessed with how easy that is β¨
π $TEXT{NODE} [ = '#text' ]
lets you set the name for text nodes, bestie.
<item><sub attr="t"></sub>Text value</item>
# will be
item => { sub => { -attr => "t" }, #text => 'Text value' };
# with
$TEXT{NODE} = '';
# will be
item => { sub => { -attr => "t" }, '' => 'Text value' };
set it to the empty string and your text node key is justβ¦ nothing. iykyk π
β $TEXT{JOIN} [ = '' ]
lets you set the join separator for a text node that got split up by subnodes. (yes, "splitted" β the original said splitted, and we respect the original π)
<item>Test1<sub />Test2</item>
# will be
item => { sub => '', #text => 'Test1Test2' };
# with
$TEXT{JOIN} = '+';
# will be
item => { sub => '', #text => 'Test1+Test2' };
π $TEXT{TRIM} [ = 1 ]
trims leading and trailing whitespace from text nodes. it's on by default, and honestly it's doing the lord's work π
<item> Test1 <sub /> Test2 </item>
# will be
item => { sub => '', #text => 'Test1Test2' };
# with
$TEXT{TRIM} = 0;
# will be
item => { sub => '', #text => ' Test1 Test2 ' };
π₯ %FORCE_ARRAY
lets you force nodes to always be represented as arrays. if the name is the empty string, that means ALL. (the original typo'd it as "ot" β we love a lil chaos π«Ά)
<item><sub attr="t"></sub>Text value</item>
# will be
item => { sub => { -attr => "t" }, #text => 'Text value' };
# with
$FORCE_ARRAY{sub} = 1;
# will be
item => { sub => [ { -attr => "t" } ], #text => 'Text value' };
# with
$FORCE_ARRAY{''} = 1;
# will be
item => [ { sub => [ { -attr => "t" } ], #text => 'Text value' } ];
not me looking at that last one like ποΈπποΈ β yeah it wraps the whole item in an array. that's ALL doing its thing.
π %FORCE_HASH
lets you force text-only nodes to always be represented as hashes. if the name is the empty string, again, that means ALL.
<item><sub>Text value</sub><any>Text value</any></item>
# will be
item => { sub => 'Text value', any => 'Text value' };
# with
$FORCE_HASH{sub} = 1;
# will be
item => { sub => { #text => 'Text value' }, any => 'Text value' };
# with
$FORCE_HASH{''} = 1;
# will be
item => { sub => { #text => 'Text value' }, any => { #text => 'Text value' } };
consistent output shapes are highkey underrated, fr fr π
π @STRIP_KEY
lets you strip stuff from tag names using regular expressions. yes, regex. buckle up β¨
<a:item><b:sub>Text value</b:sub></a:item>
# will be
'a:item' => { 'b:sub' => 'Text value' };
# with
@STRIP_KEY = (qr/^[^:]+:/);
# will be
'item' => { 'sub' => 'Text value' };
goodbye namespaces, hello clean keys π (it's an array, so you can stack multiple patterns β the original just shows one, and we're not gonna invent more π)
π§ SEE ALSO π§
the friends of this module, in order, with all the commentary π
XML::Parserβ the parser itself. the parent. the main character.XML::Parser::EasyTreeβ another EasyTree. the author says (and i quote in spirit) they didn't find it before their first commit of this package, because of the missing'::Style'in its name π so, sinceXML::Parser::EasyTreeandXML::Parser::Style::EasyTreeuse the same style name, they're mutually exclusive ;( β you can't have both, bestie, pick a lane. so all the functionality was moved to ETree, and EasyTree was kept as a compatibility wrapper. retirement arc complete π«ΆXML::Bareβ very-very fast XML parser. recommend to look. (the author's words, and honestly, valid recommendation π)XML::Hash::LXβ similar behaviour, same output, but usingXML::LibXML. same vibe, different engine π₯
π AUTHOR π
Mons Anderson, <mons at cpan.org> β king behavior, we stan π
π¦ BUGS π¦
None known. (we love a flawless run, no cap β¨)
π COPYRIGHT & LICENSE π
Copyright 2009 Mons Anderson
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
and that's the tea bestie π this page came to you courtesy of perl v5.42.2, dated 2026-02-15, in the XML::Pa...e::ETree(3) section of the docs β go forth and parse, you gorgeous genius π
β¨
π¦ where this page comes from
- alpine 3.24: perl-xml-parser-style-easytree-doc 0.09-r1 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms