senik148 has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I need some help with XML::Parser, i have this code i been working on, i got it to work. but what i need is to pull all element data from <item> how can i pass them all into a hash? for example have the first <item> element title in the hash and the next one and so on..
$data->{channel}->{item}->[0]->{title}; $data->{channel}->{item}->[1]->{title}; $data->{channel}->{item}->[2]->{title}; $data->{channel}->{item}->[3]->{title};
anybody....?
#!/usr/bin/perl # import packages use XML::Simple; use LWP::Simple; # initialize object # get RSS data $raw = get('http://movies.go.com/xml/rss/intheaters.xml'); # create object $xml = new XML::Simple; $data = $xml->XMLin($raw); $title = $data->{channel}->{item}->[0]->{title}; $link = $data->{channel}->{item}->[0]->{link}; $description = $data->{channel}->{item}->[0]->{description}; print "Title: $title\n"; print "Link: $link\n\n"; print "Description: $description\n";
ruben r.

2005-12-20 Retitled by planetscape, as per Monastery guidelines
Original title: 'XML::Parser'

Replies are listed 'Best First'.
Re: XML::Parser - element data to hash?
by duff (Parson) on Dec 20, 2005 at 23:25 UTC

    Not quite sure how you want to put the data "in a hash". Do you want the item titles to be keys or values or what?

    But here's a way to get all of the titles (untested):

    @titles = map { $_->{title} } @{$data->{channel}{item}}
Re: XML::Parser - element data to hash?
by qq (Hermit) on Dec 21, 2005 at 00:25 UTC

    Are you asking for each item in a hash? If so, something like:

    my @items = @{$data->{channel}->{item}}; for my $hash ( @items ) { print 'title: ' . $hash->{title}; print 'link: ' . $hash->{link}; print 'desc: ' . $hash->{description}; }

    But for RSS, consider one of the many CPAN modules.

      Give me some of your brains. :) Thanx to both of you, the code did what i wanted. and i got the output i wanted. Thanx, i gave both of you + points. peace
      ruben r.