Prev: FAQ 5.13 How can I open a filehandle to a string?
Next: FAQ 8.47 How do I keep my own module/library directory?
From: Jane D. on 28 Jul 2010 07:06 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.
From: Ben Morrow on 28 Jul 2010 09:04 Quoth "Jane D." <janedunnie(a)gmail.com>: > 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
From: Jasper2000 on 28 Jul 2010 10:53 Thanks for that Ben, much appreciated. I can play with that now I have an idea what's going on. Thanks again!
From: Xho Jingleheimerschmidt on 28 Jul 2010 20:58
Jane D. wrote: > 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 |