Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Reading and Formatting RSS Feed

by munchie (Monk)
on Jul 02, 2003 at 15:33 UTC ( [id://270843]=perlquestion: print w/replies, xml ) Need Help??

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

I have and iPod, and I'm trying to write a program that will read RSS feeds, format them, and save them onto my iPod. I have no problem saving them to the iPod, but here's what I need help with: I've tried using XML::RSS::Parser in a program like this:
#!/usr/bin/perl -w use strict; use XML::RSS::Parser; use URI; use LWP::UserAgent; use Data::Dumper; my $ua = LWP::UserAgent->new; $ua->agent('XML::RSS::Parser Test Script'); my @places=( 'http://www.mplode.com/tima/xml/index.xml' ); my $p = new XML::RSS::Parser; foreach my $place ( @places ) { # retreive feed my $url=URI->new($place); my $req=HTTP::Request->new; $req->method('GET'); $req->uri($url); my $feed = $ua->request($req); # parse feed $p->parse( $feed->content ); # print feed title and items data dump to screen print $p->channel->{ $p->ns_qualify('title', $p->rss_namespace +_uri ) }."\n"; my $d = Data::Dumper->new([ $p->items ]); print $d->Dump."\n\n"; }
The above is the program listed in the documentation of XML::RSS::Parser. The program works fine, but I do not know how to use $p->items. In the documentation of XML::RSS::Parser, it says $p->items returns a reference to an array of hash references. How do you work with a refrence to an array of hash refrences? Refrences have always confused me, and I don't think the Camel explains it in simple enough terms. Thanks in advance,

-munchie

Replies are listed 'Best First'.
Re: Reading and Formatting RSS Feed
by pzbagel (Chaplain) on Jul 02, 2003 at 16:02 UTC

    That's a perfect reason to pick up Randal's new book: Learning Perl Objects, References & Modules. There is also perldoc perlref and perldoc perlreftut. If you are ever in doubt of what your memory structure looks like, use Data::Dumper to print it out! Here is a quick example of dereferencing an array of hashes.

    #!/usr/bin/perl -w use strict; # Data::Dumper so we can dump out datastructure later use Data::Dumper; # Let's create an anonymous array with two elements, each a reference + to an anonymous hash. my $aref=[ {a => '1', b => '2'}, {c => '3', d => '4'} ]; #Now dump each element of the array print Dumper($aref->[0]); print Dumper($aref->[1]); #Now dereference single elements, the arrow '->' tells perl to derefen +ce $aref, the you can select elements from the hash using the normal +{} syntax print "c is $aref->[1]{c}\n"; print "a is $aref->[0]{a}\n"; ## Ouput ############### $VAR1 = { 'a' => '1', 'b' => '2' }; $VAR1 = { 'c' => '3', 'd' => '4' }; c is 3 a is 1

    HTH

Re: Reading and Formatting RSS Feed
by davorg (Chancellor) on Jul 02, 2003 at 15:53 UTC
    # $p->items is a reference to an array # therefore @{$p->items} is an array. foreach (@{$p->items}) { # Now $_ is a reference to a hash. # You can get to the whole hash like this my @keys = keys %{$_}; # or access individual elements like this my $value = $_->{$keys[0]}; }
    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Reading and Formatting RSS Feed
by sauoq (Abbot) on Jul 02, 2003 at 15:51 UTC
    How do you work with a refrence to an array of hash refrences?
    my $ref = [ { foo => 'bar' }, { baz => 'qux' } ]; print $ref->[0]{foo}, "\n"; print $ref->[1]{baz}, "\n";

    BTW, It's r-e-f-e-r-e-n-c-e with 4 'e's.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Reading and Formatting RSS Feed
by gellyfish (Monsignor) on Jul 03, 2003 at 11:04 UTC

    As a complete aside and nothing to do with your real question, if all you need to do is re-format the RSS have you considered using XSLT to do this ? XSLT is supported in Perl by XML::LibXSLT ( or if you really need to XML::XSLT ;-) One advantage of going this route is that you can have a single program to format the data in several different ways and all you need to change is the stylesheet.

    /J\
    
Re: Reading and Formatting RSS Feed
by munchie (Monk) on Jul 02, 2003 at 15:51 UTC
    This is odd... I was logged in when I posted this, but it says by "Anonymous Monk"

    -munchie

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://270843]
Approved by hardburn
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2024-04-23 10:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found