XML::Simple - Processing Query

J

Jane D.

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 (@{$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.
 
U

Uri Guttman

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

useless use of quotes on the file name.

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

JD> The top bit of the XML file looks like this:

showing xml data is useless here. use Data::Dumper to see what perl data
you got back from XML::Simple. then you can delve into that with the
proper perl array/hash accessor code.

uri
 
J

Jane D.

Yes, but I still need to understand how to process it. I've used
Data::Dumper to see what the data lokos like; I simply don't quite
understand how to access it.

Yes, I've looked in the help files, etc, etc ...

I just need somebody to show me how to do it, if anybody can spare a
few minutes. If not, then I guess I'll head elsewhere.



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

useless use of quotes on the file name.

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

  JD> The top bit of the XML file looks like this:

showing xml data is useless here. use Data::Dumper to see what perl data
you got back from XML::Simple. then you can delve into that with the
proper perl array/hash accessor code.

uri
 
X

Xho Jingleheimerschmidt

Jane said:
Yes, but I still need to understand how to process it.

If you posted "it", we could help you with that.
I've used
Data::Dumper to see what the data lokos like; I simply don't quite
understand how to access it.

But we would understand, if you showed us it.

Xho
 
U

Uri Guttman

if you read from top to bottom, you should post that way too.


JD> Yes, but I still need to understand how to process it. I've used
JD> Data::Dumper to see what the data lokos like; I simply don't quite
JD> understand how to access it.

did you post the dumper data here? why not? i said the xml data is
useless since it is parsed into perl. the perl data is useful. we can't
help you since we can't see the perl data!!

uri
 
J

Jane D.

Okay, here's the first bit of the Data Dumper result, 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' => {
'diggusername' => {
'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



Hope that helps, and that somebody is able to assist. Much
appreciated.
 
J

Jasper2000

Thanks for that Ben, much appreciated. I can play with that now I have
an idea what's going on.

Thanks again!
 
X

Xho Jingleheimerschmidt

Jane said:
Okay, here's the first bit of the Data Dumper result, content edited
for brevity:

$VAR1 = {

'count' => '10',
'story' => {

In addition to what Ben said about KeyAttr, the outermost tag around the
XML is not reported by XML::Simple (unless you specify KeepRoot), so
{stories} does not appear in the data dump, but your original code was
looking for it. (Which caused it to be autovivified)

I don't know if the XML::Simple's default value of KeyAttr is all that
clever. Sometimes trying to making something too simple results in it
being more complicated.

Cheers,


Xho
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top