View Single Post
  #2 (permalink)  
Old August 19th, 2009, 10:32 PM
Leigh Leigh is offline
Registered User
 
Join Date: Aug 2009
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Data::Dumper is your friend here.
I have modified your code slightly to include some diagnostics:
Code:
use strict;
use warnings;
use XML::Simple;
use Data::Dumper;
my $LibList = XMLin('.\\library.xml', forcearray=>1, keyattr => ['id']);
print Dumper($LibList);
my $name = $LibList->{'1'}->{name};
print "Customer id " . $name . "\n";
And the structure of your $LibList appears to be:

Code:
$VAR1 = {
          'book' => {
                      'ARRAY(0x238870c)' => {
                                              'name' => [
                                                          'ABCDEF'
                                                        ]
                                            },
                      'ARRAY(0x238864c)' => {
                                              'name' => [
                                                          'ABC'
                                                        ]
                                            },
                      'ARRAY(0x23886ac)' => {
                                              'name' => [
                                                          'ABC123'
                                                        ]
                                            }
                    }
        };

I would suggest removing the 'forcearray' paramater, so that your data ends up looking like this:


Code:
$VAR1 = {
          'book' => {
                      '1' => {
                               'name' => 'ABC'
                             },
                      '2' => {
                               'name' => 'ABC123'
                             },
                      '3' => {
                               'name' => 'ABCDEF'
                             }
                    }
        };
And then your code becomes:
Code:
use strict;
use warnings;
use XML::Simple;
use Data::Dumper;
my $LibList = XMLin('.\\library.xml',  keyattr => ['id']);
print Dumper($LibList);
my $name=$LibList->{'book'}->{'1'}->{'name'};
print "Customer id " . $name . "\n";
Reply With Quote