Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I m learning to parse XML data using XML::LibXML::Reader. Is there a way to get the total number of nodes contained in the entire XML file of a particular element? My goal is to prompt the user of the advancement of the parsing process. To do this I need to know how many values will be in my while loop.

my $entry_pattern = 'XML::LibXML::Pattern'->new('/martif/text/body +/Entry'); while ($reader->nextPatternMatch($entry_pattern)) { ...

Replies are listed 'Best First'.
Re: XML::LibXML::Reader get size
by choroba (Cardinal) on Apr 26, 2018 at 21:36 UTC
    Just count the occurrences in a loop:
    my $count = 0; ++$count while $reader->nextPatternMatch($entry_pattern);

    The problem is that once the loop is finished, you're at the end of the document, so you have to reinstantiate the reader object.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      Second problem is that if the XML is very big this will make the process last much longer.

      And since you are using the pull-parser, you'll be going through quite a bit of work just to arrive at a count, then doing it all over again. Might there be some kind of recognizable keyword within the file that you could find using regular-expressions? Read the file a chunk at a time, count the number of occurrences in each chunk, add 'em up and pretend that this is your count even if it isn't exactly. Or, perhaps just as well and a great deal easier, give the user some other sign of "visible progress," such as printing a "." to the console after every some-n nodes have been processed.