homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

Read number of xml tags

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

<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)

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)

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]

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved