Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > XM::Simple - counting tags

Reply
Thread Tools

XM::Simple - counting tags

 
 
John
Guest
Posts: n/a
 
      09-06-2006
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




 
Reply With Quote
 
 
 
 
Mumia W.
Guest
Posts: n/a
 
      09-06-2006
On 09/06/2006 05:26 AM, John wrote:
> 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.

 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      09-06-2006
John <> wrote:


> 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";
-----------------------------------------


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Peter J. Holzer
Guest
Posts: n/a
 
      09-06-2006
On 2006-09-06 10:26, John <> wrote:
><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

--
_ | Peter J. Holzer | > Wieso sollte man etwas erfinden was nicht
|_|_) | Sysadmin WSR | > ist?
| | | | Was sonst wäre der Sinn des Erfindens?
__/ | http://www.hjp.at/ | -- P. Einstein u. V. Gringmuth in desd
 
Reply With Quote
 
John
Guest
Posts: n/a
 
      09-07-2006
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
counting up instead of counting down edwardfredriks Javascript 6 09-07-2005 03:30 PM
All style tags after the first 30 style tags on an HTML page are not applied in Internet Explorer Rob Nicholson ASP .Net 3 05-28-2005 03:11 PM
JSP newbie - use include, custom tags, standard tags - or what? Mike Java 3 01-09-2004 09:30 AM
RegEx to find CFML tags nested in HTML tags Dean H. Saxe Perl 0 01-03-2004 06:11 PM
Custom Tags within Custom Tags. Ranganath Java 2 10-21-2003 06:14 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57