XML::P...e::Tree(3pm)

πŸ“¦
version 0.14 Β· 4 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– NAME πŸ’–

okay so bestie, gather round: XML::Parser::Lite::Tree β€” it's a Lightweight XML tree builder ✨ (yes that's literally the whole name section, it's giving minimalism and honestly? iconic)

This is from the User Contributed Perl Documentation for XML::P...e::Tree(3pm), and no I will not be normal about it πŸŽ€


✨ SYNOPSIS ✨

the vibes, in code form:

use XML::Parser::Lite::Tree;

my $tree_parser = XML::Parser::Lite::Tree::instance();
my $tree = $tree_parser->parse($xml_data);

  OR

my $tree = XML::Parser::Lite::Tree::instance()->parse($xml_data);

so basically you can grab the parser, keep it in a variable, and call parse on it β€” OR you can be extra and chain it all in one line like the main character you are πŸ’… both slay, pick your fave 🫢


🌸 DESCRIPTION 🌸

okay so here's the tea β˜• β€” this is a singleton class for parsing XML into a tree structure. one class, one instance, very exclusive, very VIP 🧚

and you're probably like "bestie, how is this different from every other XML tree generator out there?" and I'm SO glad you asked πŸ’– it uses XML::Parser::Lite, which is a pure perl XML parser. meaning: with this module you can tree-ify simple XML without having to compile any C. no C compiler, no build drama, no crying in the terminal 😭 periodt.

for example, this XML right here:

<foo woo="yay"><bar a="b" c="d" />hoopla</foo>

parses into this tree:

{
  'children' => [
                  {
                    'children' => [
                                    {
                                      'children' => [],
                                      'attributes' => {
                                                        'a' => 'b',
                                                        'c' => 'd'
                                                      },
                                      'type' => 'element',
                                      'name' => 'bar'
                                    },
                                    {
                                      'content' => 'hoopla',
                                      'type' => 'text'
                                    }
                                  ],
                    'attributes' => {
                                      'woo' => 'yay'
                                    },
                    'type' => 'element',
                    'name' => 'foo'
                  }
                ],
  'type' => 'root'
};

look at that nesting, it's giving 🌲 tree structure, it's giving data, it's giving ate πŸ’…

so every node has a "type" key, and it's one of three things: "root", "element", or "text" ⭐

  • "root" β€” that's the document root, and the only thing it carries is an array ref called children πŸ‘‘
  • "element" β€” that's your normal tag bestie, and it holds an array ref children, a hash ref attributes, and a string name πŸŽ€
  • "text" β€” these cuties contain literally just a content string, nothing else, no notes πŸ¦‹

πŸ’… METHODS πŸ’…

  • "instance()" β€” hands you back an instance of the tree parser. that's it. that's the method. simple, clean, iconic ✨
  • "new( options... )" β€” makes you a brand new parser. valid options include "process_ns", which is the one that processes namespaces 🌈
  • "parse($xml)" β€” takes the xml sitting in $xml, parses it, and returns the tree as a hash ref 🫢

πŸŽ€ AUTHOR πŸŽ€

Copyright (C) 2004-2008, Cal Henderson, <cal@iamcal.com>

shoutout to Cal, the original author, we love and respect the legacy πŸ’œ


πŸ”₯ SEE ALSO πŸ”₯

  • XML::Parser::Lite.

like, go read that one too, it's the whole reason this module can do its thing without C compilation drama, iykyk 😌


okay that's the whole page bestie, and here's the cute lil sign-off: this doc is from perl v5.36.0, dated 2022-11-19, filed under XML::P...e::Tree(3pm) 🌸 go forth and tree-ify some XML, you've got this πŸ’–βœ¨