PDA

View Full Version : count the number of nodes in an XML document with Perl


crmpicco
11-20-2006, 04:21 PM
Is there a way in Perl to count the number of employee nodes in an XML document?

<?xml version='1.0'?>
<staff>
<employee>
... other nodes in here ...
</employee>
<employee>
... other nodes in here ...
</employee>
<employee>
... other nodes in here ...
</employee>
<employee>
... other nodes in here ...
</employee>
<employee>
... other nodes in here ...
</employee>
</staff>

KevinADC
11-20-2006, 04:36 PM
have you looked into using any of the XML modules? XML::Parser, XML::DOM, XML::Simple, etc?

FishMonger
11-20-2006, 05:39 PM
Your prior related questions used XML::Simple.
use warnings;
use strict;
use XML::Simple;

my $xml = XMLin('employees.xml');
my $count = @{$xml->{employee}};

print "We have $count employees\n";

crmpicco
11-21-2006, 10:25 AM
thanks, worked a treat

arunprakash647
09-06-2011, 01:40 PM
if we have sub nodes for employees........how do v count the sub nodes????????