XML::XP...MLParser(3)

πŸ’– NAME πŸ’–

XML::XPath::XMLParser β€” the default XML parsing class that produces a node tree. bestie, it's the one that shows up when nobody else does 🫢


✨ SYNOPSIS ✨

my $parser = XML::XPath::XMLParser->new(
            filename => $self->get_filename,
            xml => $self->get_xml,
            ioref => $self->get_ioref,
            parser => $self->get_parser,
        );
my $root_node = $parser->parse;

πŸ¦‹ DESCRIPTION πŸ¦‹

okay so here's the vibe: this module builds you a whole node tree, and that tree becomes the context node for XPath processing. she's not trying to be fancy β€” the goal is quick parser, that's it, that's the whole personality. but lowkey she has to store way more info than most parsers do, which is the tradeoff πŸ’…

and how does she do it? array refs everywhere β€” no hashes. literally none. the author does not have performance figures for the speedups achieved, so β€” and this is a direct quote of the energy β€” no apologies to anyone not used to using arrays instead of hashes. ngl they make good sense here, since we already know the attributes of each type of node. periodt.

🌸 Node Structure 🌸

every single node has the same first 2 entries in the array: node_parent and node_pos. the type of the node is figured out using the ref() function. node_parent always holds an entry for the parent of the current node β€” except for the root node, which has undef in there. and node_pos is the position of this node in the array that it's in (think:

$node == $node->[node_parent]->[node_children]->[$node->[node_pos]]

)

nodes are structured like this πŸ‘‡

πŸ‘‘ Root Node

the root node is just an element node with no parent.

[
  undef, # node_parent - check for undef to identify root node
  undef, # node_pos
  undef, # node_prefix
  [ ... ], # node_children (see below)
]

πŸŽ€ Element Node

[
  $parent, # node_parent
  <position in current array>, # node_pos
  'xxx', # node_prefix - namespace prefix on this element
  [ ... ], # node_children
  'yyy', # node_name - element tag name
  [ ... ], # node_attribs - attributes on this element
  [ ... ], # node_namespaces - namespaces currently in scope
]

πŸ“ Attribute Node

[
  $parent, # node_parent - the element node
  <position in current array>, # node_pos
  'xxx', # node_prefix - namespace prefix on this element
  'href', # node_key - attribute name
  'ftp://ftp.com/', # node_value - value in the node
]

🌈 Namespace Nodes

every element gets its own lil set of namespace nodes that are currently in scope. each namespace node stores a prefix and the expanded name (retrieved from the xmlns:prefix="..." attribute).

[
  $parent,
  <pos>,
  'a', # node_prefix - the namespace as it was written as a prefix
  'http://my.namespace.com', # node_expanded - the expanded name.
]

πŸ’œ Text Nodes

[
  $parent,
  <pos>,
  'This is some text' # node_text - the text in the node
]

πŸ’€ Comment Nodes

[
  $parent,
  <pos>,
  'This is a comment' # node_comment
]

🧚 Processing Instruction Nodes

[
  $parent,
  <pos>,
  'target', # node_target
  'data', # node_data
]

⭐ Usage ⭐

if you feel the need to use this module outside of XML::XPath (say, you wanna use it directly so you can cache parsed trees β€” valid, iconic), here's the API you can follow:

🫢 new

the new method takes either no parameters, or any of these:

        filename
        xml
        parser
        ioref

familiar hash syntax, so an example might be:

use XML::XPath::XMLParser;

my $parser = XML::XPath::XMLParser->new(filename => 'example.xml');

the parameters mean: a filename, a string containing XML, an XML::Parser instance, and an open filehandle ref β€” in that order, respectively. you can also set or get all of these properties using the get_ and set_ functions that have the same name as the property: e.g. get_filename, set_ioref, etc. cute, right?

πŸ”₯ parse

the parse method generally takes no parameters, but you're free to pass either an open filehandle reference or an XML string if you so require. the return value is a tree that XML::XPath can use.

⚠️ heads up bestie: parse will die if there's an error in your XML. so be sure to use perl's exception handling mechanism (eval{};) if you want to avoid that. no cap.

🌟 parsefile

parsefile is identical to parse() except it expects a single parameter that's a string naming a file to open and parse. again, returns a tree, and again β€” it dies if there are XML errors. you've been warned πŸ’…


πŸ’Œ NOTICES πŸ’Œ

this file is distributed as part of the XML::XPath module, and is copyright 2000 Fastnet Software Ltd. please see the documentation for the module as a whole for licencing information.


and that's the whole page, babe πŸŽ€ this one came to you from perl v5.42.1, dated 2026-04-21, in the User Contributed Perl Documentation collection β€” love her, read her, go parse something βœ¨πŸ’–

πŸ“¦ where this page comes from