XM::Simple - counting tags

J

John

Hi

After parsing with XML::Simple the variable $xml contains:

<city>
<firm>
<employee>Fred</employee>
<employee>Bill</employee>
<employee>Bob</employee>
</firm>
</city>

I need to know how many employees? I can easily extract each
employeee but I only need the count. Is there an easier way?

Regards
John
 
M

Mumia W.

Hi

After parsing with XML::Simple the variable $xml contains:

<city>
<firm>
<employee>Fred</employee>
<employee>Bill</employee>
<employee>Bob</employee>
</firm>
</city>

I need to know how many employees? I can easily extract each
employeee but I only need the count. Is there an easier way?

Regards
John

You can probably use the 'ForceArray => 1' option when parsing
the file then count the number of elements in the array.
 
T

Tad McClellan

I need to know how many employees?


That is a different question from the one in your Subject header.

There are 6 employee "tags".

There are 3 employee "elements".

See the XML FAQ:

http://xml.silmaril.ie/authors/makeup/

I can easily extract each
employeee but I only need the count. Is there an easier way?


-----------------------------------------
#!/usr/bin/perl
use warnings;
use strict;
use XML::Simple;

my $xml = '<city>
<firm>
<employee>Fred</employee>
<employee>Bill</employee>
<employee>Bob</employee>
</firm>
</city>
';

my $ref = XMLin( $xml );

my $count = @{ $ref->{firm}{employee} }; # See "Use Rule 1" in perlreftut.pod

print "there are $count employee elements\n";
 
P

Peter J. Holzer

<city>
<firm>
<employee>Fred</employee>
<employee>Bill</employee>
<employee>Bob</employee>
</firm>
</city>

That's nice, but if it does it already contained that before parsing,
too. Parsing the above data with XML::Simple results in:

$VAR1 = {
'firm' => {
'employee' => [
'Fred',
'Bill',
'Bob'
]
}
};

I need to know how many employees?

Isn't that quite obvious from the data structure?

scalar @{ $VAR1->{firm}{employee} }

I can easily extract each employeee but I only need the count. Is
there an easier way?

An easier way than what?

hp
 
J

John

Hi

Thanks Mumia. When you said *forcearray* I realised my mistake.

I had been looking at an XML schema all morning with < and > everywhere and
had forgotten
that the returned value from XML::Simple was a *hash*. Hence as Tad says, I
was thinking of
*tags* but meant elements. Once I realised it was a hash, it was, as Peter
says, *obvious*.

So for those following the thread the complete answer would be:

my $xml=new XML::Simple (ForceArray => 1, suppressempty => 1); # create
object
my $data=$xml->XMLin("<needed>$request</needed>"); # read XML string
my @emp=@{$data->{'city'}->[0]->{'firm'}->[0]->{'employee'}};
my $no=scalar(@emp);

Sometimes, when your mind is focussed in one direction it is difficult to
see the problem.
Thanks Tad and Danke Peter. (I need to add [0] since I need to forcearray
for all the data).

Regards
John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top