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

need to retrieve xml tags

 5:40 am on Jun 9, 2009 (gmt 0)

I am new to perl
i have an xml file some thing like
<title> aaa</title>

I can hardcode and retrieve the value of childtag "title"

i.e my @attr= $root->{entry}->[0]->{title}->[0];

print $attr[0];

prints desired result i.e "ccc"

I need to know how can i retrieve it dynamically i.e instead of "0" i need to use some variable which can keep track of "entry" present in response

i need something like
my @attr= $root->{entry}->[i]->{title}->[i];

Can anyone please tell me

[edited by: PankajBansal at 5:41 am (utc) on June 9, 2009]



 9:08 am on Jun 9, 2009 (gmt 0)

welcome to WebmasterWorld [webmasterworld.com], PankajBansal!

you probably want to use a scalar variable as an index for the array.
for example:

for ($i=0; $i<@$root->{entry}; $i++)
my @attr = $root->{entry}->[$i]->{title}->[0];

welcome to perl as well - there are several resources listed in the Perl Server Side CGI Scripting forum Charter [webmasterworld.com] which will help you understand and use perl.

i should also mention that it looks like your xml sample is not "well formed", so that may cause some problems.
you may wish to seek further help with that in the XML Development [webmasterworld.com] forum or perhaps in this case the RSS, ATOM, and Related Technologies [webmasterworld.com] forum.


 5:34 am on Jun 10, 2009 (gmt 0)

btw below code helped
my $root = XMLin($xmlcontent, ForceArray=>1,KeyAttr => {});
foreach my $entry (@{$root->{entry}})
print $entry->{id}->[0];



 3:33 pm on Jun 11, 2009 (gmt 0)

Hi PankajBansal,

If you are going to be doing a lot of XML parsing and you want it to be flexible, you might want to consider one of the available perl XML libraries such as XML::LibXML to help you out. You could do something like this:

my $parser = XML::LibXML->new();
my $tree = $parser->parse_string($xml, {recover=>1});

my $root = $tree->getDocumentElement;

my @titles = $root->getElementsByTagName('Title');

foreach my $data (@titles) {
my $title = $data->getFirstChild->getData;

**Note that in the above, $xml is the XML data passed into the LibXML parser. Depending on where you are getting it from (flat-file, code generated, API response, etc.), that might affect how you need to 'prepare' it for parsing and passing in as the $xml variable.


 7:19 pm on Jun 11, 2009 (gmt 0)

welcome to WebmasterWorld [webmasterworld.com], mattdw!


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

thanks mattdw

but when i try to use use XML::LibXML; module, i get a error message
which says
Can't locate XML/LibXML.pm in @INC (@INC contains


 10:25 am on Jun 12, 2009 (gmt 0)

the above code suggested is not helping me

for ($i=0; $i<@$root->{entry}; $i++)
my @attr = $root->{entry}->[$i]->{title}->[0];

it gives me an error something like
Not an array reference


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


You need to make sure that module is installed first (from CPAN or PPM if you are on Windows) before you can use it. You'll also need to include a line like this at the top of your code to pull in the module:

use XML::LibXML;

Hope that helps!

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