"XML::Twig" is implictly recursive. You don't really need to do any more recursion. But I also don't think you're doing anything anywhere near as complicated as that:

#!/usr/bin/env perl use strict; use warnings; use XML::Twig; use Data::Dumper; sub print_book { my ( $twig, $book ) = @_; my %this_book = map { $_ -> tag, $_ -> text } $book -> children; print Dumper \%this_book; $twig -> purge; } my $twig = XML::Twig -> new ( 'twig_handlers' => { 'Book' => \&print_book } ); $twig -> parsefile ( 'your_xml_file' );

This runs through your your XML; and turns each `Book` into a hash. (And then Dumps it, but you can do something more useful). Or have I missed something profound about what you're trying to accomplish? Output from the above looks a bit like:

$VAR1 = { 'Title' => 'The age of rocks', 'Author' => 'Mary', 'Released' => '8/16/1944' };

Alternatively, you can simply use the "_all_" handler, and test each node for having children:

sub handle_node { my ( $twig, $element ) = @_; unless ( $element -> has_children ) { print "(", $element -> parent -> tag, ") ", $element -> tag, ": ", $element -> text,"\n"; } $twig -> purge; } my $twig = XML::Twig -> new ( 'twig_handlers' => { '_all_' => \&handle_node } ); $twig -> parsefile ( 'yourfile');

This will traverse all the nodes, printing any that don't have children, and purging to free up memory. With your sample data, this prints:

(Book) Title: The book of books (Book) Author: Sally (Book) Released: 1/2/2008 (Book) Title: The page of pages (Book) Author: Amanda (Book) Released: 6/3/1998 (Book) Title: The book of pages (Book) Author: John (Book) Released: 6/22/1963 (Book) Title: The rock of ages (Book) Author: Frank (Book) Released: 5/21/2004 (Book) Title: The age of rocks (Book) Author: Mary (Book) Released: 8/16/1944

In reply to Re: Is it possible to parse an XML file recursively using XML::Twig? by Preceptor
in thread Is it possible to parse an XML file recursively using XML::Twig? by Ppeoc

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.