Search code examples
perlxml-libxml

dump XML::LibXML elements


I would like to dump the xml doc tree similar to the way perl data types can be dumped.

use Data::Dumper;
print Dumper($foo);

Recursivly dumps the structure contained by $foo (even if there are cyclic dependencies).

However

use XML::LibXML;
my $parser = XML::LibXML->new();
my $doc    = $parser->parse_file($filename);
my @nodes  = $doc->findnodes($path);
foreach(@nodes)
{
    print Dumper($_);
}

just prints things like

$VAR1 = bless( do{\(my $o = 46232224)}, 'XML::LibXML::Element' );

I would like to get a tree of LibXML - elements that represents the XML-structure.


Solution

  • You could use serialize method of LibXML::Node:

    use XML::LibXML;
    my $parser = XML::LibXML->new();
    my $doc    = $parser->parse_file($filename);
    my @nodes  = $doc->findnodes($path);
    foreach my $node (@nodes)
    {
        print $node->serialize;
    }