makrospex has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,
I have one question, regarding XML::Smart. I couldnt find the answer on the net:
How can i get the number of elements of a node?

I tried

$numDefs=scalar(@{$offers->{Offer}[$i]{Definition}{ID}});

but $numDefs is always 1 (one), regardless if there is an <ID></ID> tag inside the <Definition> node(s) or not.

Is there an convenient solution to get the number of elements under a certain node?

Thanks for help and keep up the brilliant work,
makro

Replies are listed 'Best First'.
Re: how to get the number of elements with XML::Smart
by shmem (Chancellor) on Jul 17, 2007 at 18:06 UTC
    Dump $offers with Data::Dump::Streamer (preferred) or Data::Dumper to see how that structure is organized. I guess your [$i] is bogus, but I can't tell without sample data.

    You could stuff a dump of insensitive data into your scratchpad for further examination, and ask in the chatterbox...

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      Hi shmem,

      as the scalar() always returns 1 i now check with XML::Smart::null() for child elements of a node in the XML String ($offers as XML::Smart object).

      if null() returns true, i set $num to zero; else i get the correct number of child elements from scalar();

      many thanks anyway!

      regards,
      makro