XML::Simple - Processing Query

Jane D.

Usenet Archive
Joined
Jul 27, 2009
Messages
5
Reaction score
0
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.
 

Uri Guttman

Usenet Archive
Joined
Dec 16, 2000
Messages
3,126
Reaction score
0
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
 

Jane D.

Usenet Archive
Joined
Jul 27, 2009
Messages
5
Reaction score
0
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
 

Uri Guttman

Usenet Archive
Joined
Dec 16, 2000
Messages
3,126
Reaction score
0
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
 

Jane D.

Usenet Archive
Joined
Jul 27, 2009
Messages
5
Reaction score
0
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.
 

Ben Morrow

Usenet Archive
Joined
Oct 26, 2003
Messages
4,961
Reaction score
0
Quoth "Jane D." :
Okay, here's the first bit of the Data Dumper result, content edited for brevity:

Please wrap your lines to 76 characters, and reformat the data so it's comprehensible when wrapped like that. I don't know what you did to cause the key indentation to get so confused; whatever it was, please don't do it again. (DDumper output is not, by default, formatted very well. I prefer Data::Dump for this reason.)

$VAR1 = {

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

'16647039' => {
'link' => 'http://www...somelink.html', 'topic' => { 'Health' => { 'short_name' => 'health' } ...

You need to re-read the section on 'KeyAttr' in XML::Simple. Your <story>s have been flattened into a hashref, meaning you lose the order but can look up stories by their 'id' attribute. If you just want to process the stories in the order they are in the file, you want to specify KeyAttr => (and probably ForceArray => 1 as well), in which case you will get a structure like

$data = {
count => 10,
story => [
id => '16647039',
link => '...',
...,
],
...,
}

which you can iterate over with

for (@{$data->{story}}) {

Ben
 

Jasper2000

Usenet Archive
Joined
Feb 25, 2010
Messages
8
Reaction score
0
Thanks for that Ben, much appreciated. I can play with that now I have an idea what's going on.

Thanks again!
 

Xho Jingleheimerschmidt

Usenet Archive
Joined
Jan 14, 2009
Messages
213
Reaction score
0
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
548,049
Messages
3,104,245
Members
248,133
Latest member
DujonT

Latest Threads

Top