Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Open Source > Perl
|
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Perl section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 13th, 2009, 01:24 PM
Authorized User
 
Join Date: Nov 2007
Posts: 33
Thanks: 2
Thanked 0 Times in 0 Posts
Question 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?

Thanks,
 
Old August 19th, 2009, 10:32 PM
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";





Similar Threads
Thread Thread Starter Forum Replies Last Post
Parsing the xml which is having vikkiefd XML 14 July 28th, 2008 05:21 AM
parsing xml kri_hegde XML 5 July 24th, 2007 11:37 AM
XML parsing denzil_cactus Perl 0 June 11th, 2007 02:34 AM
XML Parsing tgopal Javascript 2 July 27th, 2004 08:54 AM
XML Parsing tgopal .NET Web Services 1 June 15th, 2004 03:25 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.