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

Howdy Monks. I am trying to parse some NewsML files using Syndication::NewsML. I want to extract the headline(s). Should be simple enought, right?

According to the NewsML docs at SourceForge you do this by getting ahold of the NewsLines interface for the NewsComponent you are interested in. You then use method getHeadLineCount to get the number of headlines, then use method getHeadLine(index) to get a particular headline in the list.

My relevant bit of code to do this is

if (my $newslines = $newscomp->getNewsLines) { print " Headlines:\n"; my $headcount = $newslines->getHeadLineCount(); for my $i (0..$headcount-1) { print $newslines->getHeadLine($i),"\n"; } }

When I run this it throws an error on the print statement that says: "HeadLine can be a multi-element field: must call getHeadLineList at readstories.pl line 106"

I tried using the suggested method and retrieving the results into a list, like so:

my @headlines = $newslines->getHeadLineList(); foreach my $h (@headlines) { print $h->getHeadLine(),"\n"; }

But it doesn't seem to like that either. Can anyone tell me where I'm going wrong?

Tia...

Steve

Replies are listed 'Best First'.
Re: Getting headlines from NewsML
by zentara (Cardinal) on Aug 27, 2005 at 11:08 UTC
    Try printing out the arrays and objects to narrow down where it is failing.
    print "$newslines\n"; my @headlines = $newslines->getHeadLineList(); print "@headlines\n"; foreach my $h (@headlines) { print "$h\n"; print $h->getHeadLine(),"\n"; }

    I'm not really a human, but I play one on earth. flash japh
      Doing this identified the object in @headlines as Syndication::NewsML::HeadLine=HASH(0x25055e8). In other words it was already a headline, so of course $h->getHeadLine wouldn't work. I needed to $h->getText and everything worked fine. So the working code segment is:
      if (my $newslines = $newscomp->getNewsLines) { my @headlines = $newslines->getHeadLineList(); foreach my $h (@headlines) { print "$h\n"; print $h->getText(),"\n"; } }