Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Open Source > Perl
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old August 13th, 2009, 01:24 PM
Authorized User
 
Join Date: Nov 2007
Location: , , .
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,
Reply With Quote
  #2 (permalink)  
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";
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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



All times are GMT -4. The time now is 12:17 PM.


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