XM::Simple - counting tags

Discussion in 'Perl Misc' started by John, Sep 6, 2006.

  1. John

    John Guest

    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
    John, Sep 6, 2006
    #1
    1. Advertising

  2. John

    Mumia W. Guest

    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.
    Mumia W., Sep 6, 2006
    #2
    1. Advertising

  3. 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
    Tad McClellan, Sep 6, 2006
    #3
  4. 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
    Peter J. Holzer, Sep 6, 2006
    #4
  5. John

    John Guest

    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
    John, Sep 7, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dean H. Saxe
    Replies:
    0
    Views:
    1,023
    Dean H. Saxe
    Jan 3, 2004
  2. Rob Nicholson
    Replies:
    3
    Views:
    717
    Rob Nicholson
    May 28, 2005
  3. Ranganath

    Custom Tags within Custom Tags.

    Ranganath, Oct 17, 2003, in forum: Java
    Replies:
    2
    Views:
    453
    Ranganath
    Oct 21, 2003
  4. Mike
    Replies:
    3
    Views:
    870
    Michael Borgwardt
    Jan 9, 2004
  5. edwardfredriks

    counting up instead of counting down

    edwardfredriks, Sep 6, 2005, in forum: Javascript
    Replies:
    6
    Views:
    197
    Dr John Stockton
    Sep 7, 2005
Loading...

Share This Page