homepage Welcome to WebmasterWorld Guest from 54.227.41.242
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
PankajBansal




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

I am new to perl
i have an xml file some thing like
<feed>
<title> aaa</title>
<entry>bbb</entry>
<title>ccc</title>
</entry>
<entry>#*$!</entry>
<title>yyy</title>
</entry>
</feed>

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]

 

phranque




msg:3929384
 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.

PankajBansal




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

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

}

mattdw




msg:3931330
 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.

phranque




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

welcome to WebmasterWorld [webmasterworld.com], mattdw!

PankajBansal




msg:3931791
 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

PankajBansal




msg:3931960
 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

mattdw




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

PankajBansal,

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