Welcome to WebmasterWorld Guest from 50.16.112.199

Forum Moderators: coopster & jatar k & phranque

need to retrieve xml tags

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

5+ Year Member



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]

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

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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)

5+ Year Member



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

5+ Year Member



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)

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld [webmasterworld.com], mattdw!
6:05 am on Jun 12, 2009 (gmt 0)

5+ Year Member



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)

5+ Year Member



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)

5+ Year Member



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!

 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month