On 28 July 2010 01:31, Jane D. <[email protected]> wrote:
>
> I'm basically retrieving an XML file from Digg. I can retrieve the
> Digg data okay, but am struggling with trying to process the returned
> data with Perl, largely on account with my unfamiliarity with XML
> processing, but also because I'm not entirely confident with handling
> arrays like this. Here's a stripped down version of the main bit of
> code:
>
> foreach my $record (@{$data->{stories}->[0]->{story}})

It's BC (Before Coffee) answer, but you're referring here to element 0
in "stories", which means you'll only get the first item in the
stories array.
I suggest dumping $data by adding 'use Data:Dumper; print
Dumper($data);' once you parse the XML and seeing what the structure
looks like, then follow it with your loop.

Now, the actual XML looks like an RSS feed, which begs the suggestion
to use an RSS parser module. Try XML::RSS
(http://search.cpan.org/~shlomif/XML-RSS-1.48/lib/XML/RSS.pm) or one
of its kind. It will give you simpler, documented access to all the
different attributes and elements of the XML

> The top bit of the XML file looks like this:
>
>  <?xml version="1.0" encoding="UTF-8" ?>
> - <stories count="10" offset="0" timestamp="1280259772" total="137"
> version="1.0">
> - <story link="http://www....net/..."; submit_date="1280235019"
> diggs="1" id="22914259">
>  <description>Description of story ...</description>
>  <title>Story title in here</title>
>  <user icon="/users/eprnetwork/l.png" profileviews="12780"
> registered="1170875777" />
>  <topic name="World News" short_name="world_news" />
>  <container name="World & Business" short_name="world_business" />
>  <shorturl short_url="http://digg.com/d31Y92p"; view_count="0" />
>  </story>
> - <story link="http://www..."; submit_date="1252351084" diggs="2"
> id="15603146">
>  <description>Story 2 description</description>
> etc, etc
>
>
> My issue is that I simply cannot seem to access the data in my loop,
> which it does once and then exits (when it should be doing it for each
> story). I also tried with ForceArray=0, but same issue. However, the
> data in $diggdata is definitely there.
>
> Additionally, I really need to know how to access the attributes of
> the XML as well; for example, how to get the "link" attribute, or the
> "id" attribute of the <story> tags.
>
> Any help would be much appreciated.
>
> Thanks.
>
>
> --
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
> http://learn.perl.org/
>
>
>



-- 
Erez

Observations, not opinions

--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/


Reply via email to