in reply to Is it possible to parse an XML file recursively using XML::Twig?

I like XPath and found some XPath solutions to the problem on stackoverflow here including this one: //*[not(child::*)]. XML::Twig didn't seem to handle the XPath expressions on stackoverflow but XML::XPath did. XML::XPath also comes with an 'xpath' grep like utility that might give you a solution as simple as:

xpath -e '//*[not(child::*)]' books.xml

If you want to do Perl coding for further processing the solution is still pretty simple

use strict; use warnings; use XML::XPath; use XML::XPath::XMLParser; my $xp = XML::XPath->new(filename => 'books.xml'); my $nodeset = $xp->find('//*[not(child::*)]'); foreach my $node ($nodeset->get_nodelist) { print "FOUND:", XML::XPath::XMLParser::as_string($node), "\n"; }
Ron

Replies are listed 'Best First'.
Re^2: Is it possible to parse an XML file recursively using XML::Twig?
by Jenda (Abbot) on Oct 21, 2015 at 16:27 UTC

    Please corect me if I'm wrong but ... this will parse the whole (huge) file and produce an even huger maze of objects in memory before you even get a chance to call your find(). A huge waste of memory for such a task.

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

Re^2: Is it possible to parse an XML file recursively using XML::Twig?
by Ppeoc (Beadle) on Oct 30, 2015 at 17:57 UTC
    Ron, I really like your solution! I was using XML::parse so far. But this one definitely seems to work. Thank you