How can I parse a huge highly nested XML document to display each leaf element. I want to do this recursively where the child is thrown into the recursive function the leaf is found. Is this possible using XML:TWIG?
Here is a sample code I want to parse
<?xml version="1.0" encoding="UTF-8"?> -<ArrayOfBooks> -<Book> <Title>The book of books</Title> <Author>Sally</Author> <Released>1/2/2008</Released> </Book> -<Book> <Title>The page of pages</Title> <Author>Amanda</Author> <Released>6/3/1998</Released> </Book> <Book> <Title>The book of pages</Title> <Author>John</Author> <Released>6/22/1963</Released> </Book> <Book> <Title>The rock of ages</Title> <Author>Frank</Author> <Released>5/21/2004</Released> </Book> <Book> <Title>The age of rocks</Title> <Author>Mary</Author> <Released>8/16/1944</Released> </Book> </ArrayOfBooks>
Say I want to parse all the children in the snippet code below. I want to recursively parse each node till the last child is reached. I have no information about the nodes as they are dynamic. My actual file is highly nested. How can I do this using Twig?
my $twig = XML::Twig->new( twig_handlers => { '/ArrayOfBooks/Book' => +\&sect} ); my $input ='Books.XML'; $twig->parsefile($input); sub sect { my ($twig, $ele) = @_; depth++ #if node does not contain children { end_element($ele); depth--; return; } #node contains children sec($ele); } sub end_element { # I need both key and value. eg Title: The page of pages my ($leaf) = @_; print $key; }

In reply to 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.