NAME

XML::PP - A simple XML parser

VERSION

Version 0.07

SYNOPSIS

use XML::PP;

my $parser = XML::PP->new();
my $xml = '<note id="1"><to priority="high">Tove</to><from>Jani</from><heading>Reminder</heading><body importance="high">Don\'t forget me this weekend!</body></note>';
my $tree = $parser->parse($xml);

print $tree->{name};  # 'note'
print $tree->{children}[0]->{name};   # 'to'

DESCRIPTION

You almost certainly do not need this module. For most tasks, use XML::Simple or XML::LibXML. XML::PP exists only for the most lightweight scenarios where you can't get one of the above modules to install, for example, CI/CD machines running Windows that get stuck with https://stackoverflow.com/questions/11468141/cant-load-c-strawberry-perl-site-lib-auto-xml-libxml-libxml-dll-for-module-x.

XML::PP is a simple, lightweight XML parser written in pure Perl. It does not rely on external libraries like XML::LibXML and is suitable for small XML parsing tasks. This module supports basic XML document parsing, including namespace handling, attributes, and text nodes.

METHODS

new

my $parser = XML::PP->new();
my $parser = XML::PP->new(strict => 1);
my $parser = XML::PP->new(warn_on_error => 1);

Creates a new XML::PP object. It can take several optional arguments:

parse

my $tree = $parser->parse($xml_string);

Parses the XML string and returns a tree structure representing the XML content. The returned structure is a hash reference with the following fields:

collapse_structure

Collapse an XML-like structure into a simplified hash (like XML::Simple).

use XML::PP;

my $input = {
    name => 'note',
    children => [
        { name => 'to', children => [ { text => 'Tove' } ] },
        { name => 'from', children => [ { text => 'Jani' } ] },
        { name => 'heading', children => [ { text => 'Reminder' } ] },
        { name => 'body', children => [ { text => 'Don\'t forget me this weekend!' } ] },
    ],
    attributes => { id => 'n1' },
};

my $result = collapse_structure($input);

# Output:
# {
#     note => {
#         to      => 'Tove',
#         from    => 'Jani',
#         heading => 'Reminder',
#         body    => 'Don\'t forget me this weekend!',
#     }
# }

The collapse_structure subroutine takes a nested hash structure (representing an XML-like data structure) and collapses it into a simplified hash where each child element is mapped to its name as the key, and the text content is mapped as the corresponding value. The final result is wrapped in a note key, which contains a hash of all child elements.

This subroutine is particularly useful for flattening XML-like data into a more manageable hash format, suitable for further processing or display.

collapse_structure accepts a single argument:

The subroutine returns a hash reference that represents the collapsed structure, where the top-level key is note and its value is another hash containing the child elements' names as keys and their corresponding text values as values.

For example:

{
    note => {
        to      => 'Tove',
        from    => 'Jani',
        heading => 'Reminder',
        body    => 'Don\'t forget me this weekend!',
    }
}

_parse_node

my $node = $self->_parse_node($xml_ref, $nsmap);

Recursively parses an individual XML node. This method is used internally by the parse method. It handles the parsing of tags, attributes, text nodes, and child elements. It also manages namespaces and handles self-closing tags.

AUTHOR

Nigel Horne, <njh at nigelhorne.com>

SEE ALSO

SUPPORT

This module is provided as-is without any warranty.

LICENSE AND COPYRIGHT

Copyright 2025 Nigel Horne.

Usage is subject to licence terms.

The licence terms of this software are as follows: