p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Perl (http://p2p.wrox.com/forumdisplay.php?f=196)
-   -   XML::Simple parsing xml (http://p2p.wrox.com/showthread.php?t=75696)

geek.shrek August 13th, 2009 01:24 PM

XML::Simple parsing xml
 
Hi,

First, I'm sorry if this is not the right forum.

I'm trying to parse XML file with XML::Simple in perl.

Here's my input
Code:

<Library>
    <book>
            <id>1</id>
            <name>ABC</name>
    </book>
    <book>
            <id>2</id>
            <name>ABC123</name>
    </book>
    <book>
            <id>3</id>
            <name>ABCDEF</name>
    </book>
</Library>

I would like to get the name of id '1'.

Code:

my $LibList = XMLin('library.xml', forcearray=>1, keyattr => ['id']);

my $name = $LibList->{'1'}->{name};
print "Customer id " . $name . "\n";

It doesn't return anything.

Can someone tell me what did I do wrong? [:confused:]

Thanks,

Leigh August 19th, 2009 10:32 PM

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";



All times are GMT -4. The time now is 09:00 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.