Home > Archive > PERL Beginners > March 2006 > Counting specific elements in a XML object
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Counting specific elements in a XML object
|
|
| Dave Adams 2006-03-30, 6:57 pm |
| If I have a xml file like the following:
<?xml version='1.0'?>
<employee>
<name>John Doe</name>
<age>43</age>
<sex>M</sex>
<department>Recieving</department>
</employee>
<employee>
<name>Bob Gordon</name>
<age>50</age>
<sex>M</sex>
<department>Shipping</department>
</employee>
Is there some perl module out there that can help me get the number of
employees or in other words, the number occurences of "<employee>"?
I guess I can get it conventionally by reading in the file and doing a count
every time the script encounters "<name>". But there must be an easier way..
Any ideas?
DA
| |
| Hans Meier 2006-03-30, 6:57 pm |
| Dave Adams am Donnerstag, 30. M=C3=A4rz 2006 21.12:
> If I have a xml file like the following:
>
> <?xml version=3D'1.0'?>
> <employee>
> <name>John Doe</name>
> <age>43</age>
> <sex>M</sex>
> <department>Recieving</department>
> </employee>
> <employee>
> <name>Bob Gordon</name>
> <age>50</age>
> <sex>M</sex>
> <department>Shipping</department>
> </employee>
>
>
> Is there some perl module out there that can help me get the number of
> employees or in other words, the number occurences of "<employee>"?
>
> I guess I can get it conventionally by reading in the file and doing a
> count every time the script encounters "<name>". But there must be an
> easier way.
Ok, let's start with:
perl -e 'while(<STDIN> ){/<employee>/&&$i++};print $i' < data.xml
Anybody with an easier way in the sense of
=2D less typing
=2D more understandable
=2D less module imports ;-)
?
[preconditions: nothing else to do with the input; no CDATA containing=20
<employees>; valid XML; etc.]
Hans
| |
| Gavin Bowlby 2006-03-30, 6:57 pm |
| How about :
cat fn | grep <string to be searched for> | wc
as a non-Perl approach to the problem...
-----Original Message-----
From: Hans Meier (John Doe) [mailto:security.department@tele2.ch]=20
Sent: Thursday, March 30, 2006 11:38 AM
To: beginners@perl.org
Subject: Re: Counting specific elements in a XML object
Dave Adams am Donnerstag, 30. M=E4rz 2006 21.12:
> If I have a xml file like the following:
>
> <?xml version=3D'1.0'?>
> <employee>
> <name>John Doe</name>
> <age>43</age>
> <sex>M</sex>
> <department>Recieving</department>
> </employee>
> <employee>
> <name>Bob Gordon</name>
> <age>50</age>
> <sex>M</sex>
> <department>Shipping</department>
> </employee>
>
>
> Is there some perl module out there that can help me get the number of
> employees or in other words, the number occurences of "<employee>"?
>
> I guess I can get it conventionally by reading in the file and doing a
> count every time the script encounters "<name>". But there must be an
> easier way.
Ok, let's start with:
perl -e 'while(<STDIN> ){/<employee>/&&$i++};print $i' < data.xml
Anybody with an easier way in the sense of
- less typing
- more understandable
- less module imports ;-)
?
[preconditions: nothing else to do with the input; no CDATA containing=20
<employees>; valid XML; etc.]
Hans
--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
| |
| Hans Meier 2006-03-30, 6:57 pm |
| Gavin Bowlby am Donnerstag, 30. M=E4rz 2006 21.45:
> How about :
>
> cat fn | grep <string to be searched for> | wc
When I posted a "cat | grep" the first (and last) time, several people got =
a=20
well known heart attack :-)
grep <string to be searched for> fn | wc
> as a non-Perl approach to the problem...
[...]
Agreed, but OT here ;-)
Hans
| |
| Chas Owens 2006-03-30, 6:57 pm |
| On 3/30/06, Hans Meier (John Doe) <security.department@tele2.ch> wrote:
> Gavin Bowlby am Donnerstag, 30. M=E4rz 2006 21.45:
>
> When I posted a "cat | grep" the first (and last) time, several people go=
t a
> well known heart attack :-)
>
> grep <string to be searched for> fn | wc
>
> [...]
>
> Agreed, but OT here ;-)
>
> Hans
If we are going to pick nits then it should be
grep -c "<employee>" fn
| |
| Hans Meier 2006-03-30, 6:57 pm |
| Chas Owens am Donnerstag, 30. M=E4rz 2006 22.35:
[...][color=darkred]
[...][color=darkred]
> If we are going to pick nits then it should be
>
> grep -c "<employee>" fn
too much work.=20
c "<employee>" fn
But your alias may be different ;-)
Hans
| |
| Bob Showalter 2006-03-30, 6:57 pm |
| Chas Owens wrote:
> If we are going to pick nits then it should be
>
> grep -c "<employee>" fn
>
Note that grep -c counts matching *lines*. There is no formal
requirement that these elements appear on separate lines.
Here's a slightly more complex Perl one-liner that counts *occurences*
perl -lne '$n++ for /<employee>/g; END {print $n}' somefile.xml
| |
| Hans Meier 2006-03-31, 3:57 am |
| Bob Showalter am Donnerstag, 30. M=E4rz 2006 23.32:
> Chas Owens wrote:
>
> Note that grep -c counts matching *lines*. There is no formal
> requirement that these elements appear on separate lines.
Dave,=20
my first one-liner suffers from the same (also only counts lines)
> Here's a slightly more complex Perl one-liner that counts *occurences*
>
> perl -lne '$n++ for /<employee>/g; END {print $n}' somefile.xml
Bob,
thanks, I thought about the -n option, but missed the END block idea to=20
finally print the sum.
(And again, problems that seem to be trivial are not in many cases...)
Hans
|
|
|
|
|