XML::Simple XMLin

Hi,

If somebody can assist me with my query it would be much appreciated.

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:

my $xml = new XML::Simple;
my $data = $xml->XMLin("$diggdata",ForceArray=>1);

foreach my $record ( [at] {$data->{stories}->[0]->{story}})
{
print $record->{title}->[0]->{content};
print $record->{description}->[0]->{content};
# etc, etc, etc
}


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: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
janedunnie [ Mi, 28 Juli 2010 00:31 ] [ ID #2045257 ]

Re: XML::Simple XMLin

On 28 July 2010 01:31, Jane D. <janedunnie [at] gmail.com> 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 ( [at] {$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:
>
> =A0<?xml version=3D"1.0" encoding=3D"UTF-8" ?>
> - <stories count=3D"10" offset=3D"0" timestamp=3D"1280259772" total=3D"13=
7"
> version=3D"1.0">
> - <story link=3D"http://www....net/..." submit_date=3D"1280235019"
> diggs=3D"1" id=3D"22914259">
> =A0<description>Description of story ...</description>
> =A0<title>Story title in here</title>
> =A0<user icon=3D"/users/eprnetwork/l.png" profileviews=3D"12780"
> registered=3D"1170875777" />
> =A0<topic name=3D"World News" short_name=3D"world_news" />
> =A0<container name=3D"World & Business" short_name=3D"world_business" />
> =A0<shorturl short_url=3D"http://digg.com/d31Y92p" view_count=3D"0" />
> =A0</story>
> - <story link=3D"http://www..." submit_date=3D"1252351084" diggs=3D"2"
> id=3D"15603146">
> =A0<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=3D0, 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: beginners-unsubscribe [at] perl.org
> For additional commands, e-mail: beginners-help [at] perl.org
> http://learn.perl.org/
>
>
>



--
Erez

Observations, not opinions

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Erez Schatz [ Mi, 28 Juli 2010 06:46 ] [ ID #2045260 ]

Re: XML::Simple XMLin

Thanks Erez ... However, I did already look at the data in
Data:Dumper, and still can't figure out how to access it. Mind you, I
have done an identical loop for other XML data, from other sources,
and it works fine (even with the element set to 0, which I don't quite
get). Mind you, if it's of any use, here is the top portion of the
data dump (content edited for brevity):


$VAR1 = {
'count' => '10',
'story' => {
'16647039' => {
'link' => 'http://www...somelink.html',
'topic' => {
'Health' => {
'short_name' =>
'health'
}
},
'status' => 'upcoming',
'submit_date' => '1256158570',
'container' => {
'Lifestyle' => {

'short_name' => 'lifestyle'
}
},
'comments' => '0',
'description' => [
'Some description in
here'
],
'diggs' => '1',
'media' => 'news',
'href' => 'http://digg.com/restofurl',
'user' => {
'socialshopping2' => {
'icon'
=> '',

'registered' => '1255514015',

'profileviews' => '105'
}
},
'shorturl' => [
{
'view_count' => '0',
'short_url' => 'http://
digg.com/restofurl'
}
],
'title' => [
'Some title of some
article'
]
},
'22914259' => {
'link' => 'http://www...next-
link.html',
'topic' => {
'World News' => {

'short_name' => 'world_news'
}
},
etc,
etc


Any help would be most appreciated.


--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Jasper2000 [ Mi, 28 Juli 2010 14:59 ] [ ID #2045261 ]

Re: XML::Simple XMLin

Rmlyc3QsIEkgdXJnZSB5b3UgdG8gdHJ5IHBhcnNpbmcgdGhpcyBkb2Mgd2l0 aCBhIGRlZGljYXRl
ZCBSU1MgcGFyc2VyLAppdCB3aWxsIGRvIG1pcmFjbGVzIGZvciB5b3VyIG5l ZWRzLiBTZWNvbmQs
IHRoZSBkdW1wZXIgeW91IHByaW50IGhlcmUKaXMgbm90IHRoZSBmdWxsIGRv Y3VtZW50LCBidXQg
b25lIGJyYW5jaCwgaXQgbWlnaHQgYmUgdGhhdCB3aGF0IHlvdQpsb29wIG92 ZXIgaXMgbm90IGFu
IGFycmF5cmVmLCBvciBtaWdodCBub3Qgd29y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AgoCCgIKAgoCCgIKAgoCCgIKAgoCCgIKAgoCCgIKAgoCCg IKAgoCCgIKAgoCCg
fQo+IKAgoCCgIKAgoCCgIKAgoCCgIKAgoCCgIKAgoCCgIKAgoCCgIKAgoCCg IKB9LAo+IKAgoCCg
IKAgoCCgIKAgoCCgIKAgoCCgIKAgoCCgIKBldGMsCj4goCCgIKAgoCCgIKAg oCCgIKAgoCCgIKAg
oCCgIKAgoGV0Ywo+Cj4KPiBBbnkgaGVscCB3b3VsZCBiZSBtb3N0IGFwcHJl Y2lhdGVkLgo+Cj4K
PiAtLQo+IFRvIHVuc3Vic2NyaWJlLCBlLW1haWw6IGJlZ2lubmVycy11bnN1 YnNjcmliZUBwZXJs
Lm9yZwo+IEZvciBhZGRpdGlvbmFsIGNvbW1hbmRzLCBlLW1haWw6IGJlZ2lu bmVycy1oZWxwQHBl
cmwub3JnCj4gaHR0cDovL2xlYXJuLnBlcmwub3JnLwo+Cj4KPgoKCgotLSAK RXJlegoKT2JzZXJ2
YXRpb25zLCBub3Qgb3BpbmlvbnMK
Erez Schatz [ Mi, 28 Juli 2010 21:22 ] [ ID #2045265 ]

Re: XML::Simple XMLin

On Tue, Jul 27, 2010 at 6:31 PM, Jane D. <janedunnie [at] gmail.com> wrote:
> The top bit of the XML file looks like this:

It would be easier for me to make suggestions if you'd post an entire
sample XML document or, better yet, a URL where I could retreive one
so that I could test my code before suggesting it to you. :)

--
Brandon McCaig <bamccaig [at] gmail.com>
V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
Castopulence Software <http://www.castopulence.org/> <bamccaig [at] castopulence.org>

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Brandon McCaig [ Do, 29 Juli 2010 00:10 ] [ ID #2045291 ]

Re: XML::Simple XMLin

Actually, I have now figured it out, and it all makes sense!

Thanks for your help.


--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
janedunnie [ Mi, 28 Juli 2010 20:45 ] [ ID #2045292 ]

Re: XML::Simple XMLin

Brandon / Erez,

Thanks very much for your help. Not sure why, but I did post another
message, to say that I'd managed to figure it out, yet the message
didn't show up.

However, I really do appreciate your comments and willingness to help
me. Thanks!!



On Jul 28, 6:10 pm, bamcc... [at] gmail.com (Brandon McCaig) wrote:
> On Tue, Jul 27, 2010 at 6:31 PM, Jane D. <janedun... [at] gmail.com> wrote:
> > The top bit of the XML file looks like this:
>
> It would be easier for me to make suggestions if you'd post an entire
> sample XML document or, better yet, a URL where I could retreive one
> so that I could test my code before suggesting it to you. :)
>
> --
> Brandon McCaig <bamcc... [at] gmail.com>
> V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
> Castopulence Software <http://www.castopulence.org/> <bamcc... [at] castopulence.org>


--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Jasper2000 [ Do, 29 Juli 2010 01:44 ] [ ID #2045293 ]
Perl » gmane.comp.lang.perl.beginners » XML::Simple XMLin

Vorheriges Thema: Compare file/dir size
Nächstes Thema: ftps module