EasyTree(3pm)

πŸ’– EasyTree πŸ’–

okay so bestie, welcome to XML::Parser::EasyTree β€” yes it's a Perl manpage, we're in our 3pm era, and it's literally serving easier tree style for XML::Parser. That's the whole name, that's the whole vibe. 🌸


✨ SYNOPSIS ✨

use XML::Parser;
use XML::Parser::EasyTree;
$XML::Parser::Easytree::Noempty=1;
my $p=new XML::Parser(Style=>'EasyTree');
my $tree=$p->parsefile('something.xml');

copy it character for character, bestie, every single one matters 🫢


🌈 DESCRIPTION 🌈

So here's the tea: XML::Parser::EasyTree adds a brand-new "built-in" style called "EasyTree" to XML::Parser. Just like XML::Parser's "Tree" style, setting this style makes the parser build a lightweight tree structure that represents the XML document. And this structure is β€” at least in this author's opinion, and honestly we respect the confidence β€” easier to work with than the one the built-in style creates. Periodt.

When the parser gets invoked with the EasyTree style, it hands you back a reference to an array of tree nodes, and every one of those nodes is a hash reference. Every node has a 'type' key whose value tells you what kind of node it is:

  • 'e' β€” element nodes
  • 't' β€” text nodes
  • 'p' β€” processing instruction nodes

Every node also has a 'content' key, whose value is:

  • for element nodes β€” a reference to an array holding the element's child nodes
  • for text nodes β€” the string value
  • for processing instruction nodes β€” the data value

Element nodes also come with an 'attrib' key, which is a reference to a hash of attribute names and values. Processing instructions also have a 'target' key whose value is the PI's target. Iconic, honestly. πŸ‘‘

EasyTree nodes are ordinary Perl hashes, not objects. And contiguous runs of text are always returned in a single node β€” no crumbs, one clean node. ✨

Now, why does the parser return an array reference instead of just the root element's node? Great question, bestie. Because an XML document can legally contain processing instructions outside the root element (the xml-stylesheet PI is commonly used this way). So it's giving you the whole picture, not just the main character. πŸŽ€

And one more thing: if the parser's Namespaces option is set, element and attribute names will be prefixed with their (possibly empty) namespace URI, all wrapped up in curly brackets. {like this} πŸ¦‹


πŸ’… SPECIAL VARIABLES πŸ’…

Two package global variables control special behaviors. Just two. Little icons.

  • XML::Parser::EasyTree::Latin β€” if you set this to a nonzero value, all text, names, and values come back in ISO-8859-1 (Latin-1) encoding instead of UTF-8. Choose your fighter.
  • XML::Parser::EasyTree::Noempty β€” if you set this to a nonzero value, text nodes containing nothing but whitespace (you know the ones β€” those little gremlins generated by line breaks and indentation between tags) get omitted from the parse tree entirely. Clean girl parse. 🧹✨

πŸ“ EXAMPLE πŸ“

Let's parse a prettyprinted version of the XML shown in the example for the built-in "Tree" style. Slay:

#!perl -w
use strict;
use XML::Parser;
use XML::Parser::EasyTree;
use Data::Dumper;

$XML::Parser::EasyTree::Noempty=1;
my $xml=<<'EOF';
<foo>
  <head id="a">Hello <em>there</em>
  </head>
  <bar>Howdy<ref/>
  </bar>
do
</foo>
EOF
my $p=new XML::Parser(Style=>'EasyTree');
my $tree=$p->parse($xml);
print Dumper($tree);

Returns:

$VAR1 = [
        { 'name' => 'foo',
          'type' => 'e',
          'content' => [
                         { 'name' => 'head',
                           'type' => 'e',
                           'content' => [
                                          { 'type' => 't',
                                            'content' => 'Hello '
                                          },
                                          { 'name' => 'em',
                                            'type' => 'e',
                                            'content' => [
                                                           { 'type' => 't',
                                                             'content' => 'there'
                                                           }
                                                         ],
                                            'attrib' => {}
                                          }
                                        ],
                           'attrib' => { 'id' => 'a'
                                       }
                         },
                         { 'name' => 'bar',
                           'type' => 'e',
                           'content' => [
                                          { 'type' => 't',
                                            'content' => 'Howdy'
                                          },
                                          { 'name' => 'ref',
                                            'type' => 'e',
                                            'content' => [],
                                            'attrib' => {}
                                          }
                                        ],
                           'attrib' => {}
                         },
                         { 'type' => 't',
                           'content' => '
do
'
                         }
                       ],
          'attrib' => {}
        }
      ];

look at that structure. look at it. gorgeous. πŸ’–


🫢 AUTHOR 🫢

Eric Bohlman (ebohlman@omsdev.com)

Copyright (c) 2001 Eric Bohlman. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

(the legal bits stay exactly as they are, bestie β€” no remixing those 🫑)


πŸ‘€ SEE ALSO πŸ‘€

  • XML::Parser

and that's the whole page, fr fr 🌈 β€” this was EasyTree(3pm) from perl v5.32.1, dated 2021-12-23. go forth and parse something, you got this πŸ’œπŸ¦‹

πŸ“¦ where this page comes from