XML::TreeBuilder(3pm)
β¨ XML::TreeBuilder β¨
hiii bestie π«Ά welcome to the User Contributed Perl Documentation page for XML::TreeBuilder. it's a Perl module that builds you a whole tree of XML::Element objects out of an XML document, and honestly? she's iconic. let's get into it π
πΈ NAME πΈ
XML::TreeBuilder β a parser that builds a tree of XML::Element objects. that's the whole name, no notes, periodt.
π¦ SYNOPSIS π¦
okay so here's the vibe in code form, copy it exactly bestie:
foreach my $file_name (@ARGV) {
my $tree = XML::TreeBuilder->new({ 'NoExpand' => 0, 'ErrorContext' => 0 }); # empty tree
$tree->parse_file($file_name);
print "Hey, here's a dump of the parse tree of $file_name:\n";
$tree->dump; # a method we inherit from XML::Element
print "And here it is, bizarrely rerendered as XML:\n",
$tree->as_XML, "\n";
# Now that we're done with it, we must destroy it.
$tree = $tree->delete;
}
π DESCRIPTION π
so this module uses XML::Parser to build XML document trees made out of XML::Element objects (and XML::Element is a subclass of HTML::Element that's been adapted for XML, iykyk). XML::TreeBuilder is specifically for the girlies who already know and love the HTML::TreeBuilder / HTML::Element interface to document trees and who do NOT want to go learn some other document interface like XML::Twig or XML::DOM. we respect the comfort zone here.
the way to use this class, step by step, no skips:
- start a new (empty)
XML::TreeBuilderobject. fresh start, clean slate β¨ - set any of the "store" options you want.
- then parse the document from a source by calling
$x->parsefile(...)or$x->parse(...)(see theXML::Parserdocs for the options those two methods take). - do whatever you need to do with the syntax tree β presumably traversing it looking for some bit of information in it.
- and finally, when you're done with the tree, call
$tree->deleteto erase the contents of the tree from memory.
β οΈ okay listen, this last one is important. this kind of thing usually isn't necessary with most Perl objects, but it is necessary for TreeBuilder objects. if you skip it you're gonna have a bad time π. see HTML::Element for a more verbose explanation of why this is the case.
π METHODS AND ATTRIBUTES π
XML::TreeBuilder is a subclass of XML::Element, which in turn is a subclass of HTML:Element. you should read and understand the documentation for those two modules, fr fr β go do your homework π
an XML::TreeBuilder object is just a special XML::Element object that lets you call these additional methods:
π $root = XML::TreeBuilder->new()
constructs a brand new XML::TreeBuilder object. main character birth moment π«
Parameters:
NoExpandβ passed toXML::Parser. do not expand external entities. Default:undefErrorContextβ passed toXML::Parser. number of context lines to generate on errors. Default:undefcatalogβ path to an Oasis XML catalog. passed toXML::Catalogto resolve entities ifNoExpandis not set. Default:$ENV{XML_CATALOG_FILES} || '/etc/xml/catalog'
π $root->eof
deletes the parser object. bye bye parser π¦
π $root->parse(...options...)
uses XML::Parser's parse method to parse XML from the source(s?) specified by the options. see XML::Parse.
π $root->parsefile(...options...)
uses XML::Parser's parsefile method to parse XML from the source(s?) specified by the options. see XML::Parse.
π $root->parse_file(...options...)
simply an alias for parsefile. same energy, different name π
π $root->store_comments(value)
this determines whether TreeBuilder will normally store comments found while parsing content into $root. currently, this is off by default.
π $root->store_declarations(value)
this determines whether TreeBuilder will normally store markup declarations found while parsing content into $root. currently, this is off by default.
π $root->store_pis(value)
this determines whether TreeBuilder will normally store processing instructions found while parsing content into $root. currently, this is off (false) by default.
π $root->store_cdata(value)
this determines whether TreeBuilder will normally store CDATA sections found while parsing content into $root. adds a ~cdata node. currently, this is off (false) by default.
π SEE ALSO π
XML::ParserXML::ElementHTML::TreeBuilderHTML::DOMbo
and for alternate XML document interfaces: XML::DOM and XML::Twig. go forth and explore, bestie π
π COPYRIGHT AND DISCLAIMERS π
Copyright (c) 2000,2004 Sean M. Burke. All rights reserved. Copyright (c) 2010,2011,2013 Jeff Fearn. All rights reserved.
this library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
this program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose. (so like, no promises, okay? π)
πΈ AUTHOR πΈ
Current Author: Jeff Fearn <jfearn@cpan.org>.
Former Authors: Sean M. Burke, <sburke@cpan.org>
and that's the whole page, bestie π«Ά this was perl v5.34.0, dated 2022-06-28, and it's XML::TreeBuilder(3pm) β go build a tree, slay, and don't forget the delete π
β¨
π¦ where this page comes from
- debian bookworm: libxml-treebuilder-perl 5.4-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libxml-treebuilder-perl 5.4-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libxml-treebuilder-perl 5.4-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libxml-treebuilder-perl 5.4-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libxml-treebuilder-perl 5.4-3 Β· 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