Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^4: Is it possible to parse an XML file recursively using XML::Twig?

by mr_ron (Chaplain)
on Oct 26, 2015 at 14:56 UTC ( [id://1145961]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Is it possible to parse an XML file recursively using XML::Twig?
in thread Is it possible to parse an XML file recursively using XML::Twig?

The following more cautious code seemed to work for me and should purge memory regularly. I worry that calling purge on every element which might purge something you still need around.

use strict; use warnings; use XML::Twig; $|++; my $twig = XML::Twig->new( twig_handlers => { # as noted in the documentation for end_tag_handlers ... # "twig_handlers are called when an element is completely pars +ed" # so should be safe to purge here 'Book' => sub { my ($twig, $el) = @_; # print "purging ...\n"; $twig->purge; }, 'Book//*' => sub { # see http://search.cpan.org/~mirod/XML-Twig-3.49/Twig.pm# +cond # for #ELT which is an element print $_->tag, ': ', $_->text, $/ unless ($_->has_children('#ELT')); } } ); $twig->parsefile('books.xml');
Ron
  • Comment on Re^4: Is it possible to parse an XML file recursively using XML::Twig?
  • Download Code

Replies are listed 'Best First'.
Re^5: Is it possible to parse an XML file recursively using XML::Twig?
by Ppeoc (Beadle) on Oct 30, 2015 at 18:40 UTC
    This is exactly what I was looking for! Thank you :) I now just need to figure how to have the full nested path for each child. Thank you once again :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1145961]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-16 05:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found