Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

Read number of xml tags



6:21 am on Jun 12, 2009 (gmt 0)

5+ Year Member

<long_desc isprivate="0">
<thetext>iam great.</thetext>
<long_desc isprivate="0">
<def> </def>
<thetext>he is better</thetext>

From teh above response i want to store the content of "thetext" in an array.
<thetext> is the child tag of <long_desc>

If you see the above code there are 2 <long_desc> tags, each has its own child <thetext>.
I need to parse the xml file, and store it in an array

something like
$myArray[0]= iam great
$myArray= He is better

I can retrieve them using something like

my $root = XMLin($xmlcontent, ForceArray=>1,KeyAttr => {});
// $xmlcontent has the response. Whole string i.e
foreach my $bug(@{$root->{bug}})
print $root->{bug}->[0]->{long_desc}->[0]->{thetext}->[0];
// prints iam great

print $root->{bug}->[0]->{long_desc}->[1]->{thetext}->[0];

//prints he is better


but the above code works because i hardcoded values i.e
i knew there were 2 long_desc tags. (i.e i usedlong_Desc->[0], long_desc[1] etc)

In real time i need to know how can i find total number of tags.

I need to do this whole thing using XML-Simple module of PERL

[1][edited by: PankajBansal at 6:35 am (utc) on June 12, 2009]


1:30 pm on Jun 12, 2009 (gmt 0)

5+ Year Member

Could you do something like this?:

my $count = @{$root->{bug}->[0]->{long_desc}};

Should give you the size of the array holding the long_desc tags.

If you did:

my @count = @{$root->{bug}->[0]->{long_desc}};

then you have all the tags in an array you can walk.


1:47 pm on Jun 12, 2009 (gmt 0)

5+ Year Member

Thanks thanks a lot Mattdw....

It worked...I spent whole day trying to get it....i was completely down and out but your code helped me....

It was simple though. Do help in future too:-)

thanks again :-)

[edited by: PankajBansal at 1:47 pm (utc) on June 12, 2009]


Featured Threads

Hot Threads This Week

Hot Threads This Month