Solved

Perl and XML::Simple

Posted on 2011-09-22
3
264 Views
Last Modified: 2012-05-12
Hi,
Please see code below. I am using XML::Simple to unravel an XML File. My end purpose to this is to extract all data out of the XML file and import the data into a database.
My question:  Is there a simple way to tell how many records I actually have in the XML file. I want to create a  'for' loop but I don't know how many iterations to give it.
I'm sure there is a simple solution here I am not seeing. Thanks!
use XML::Simple qw(:strict);
use Data::Dumper;

# $rsp contains the XML that I retrieve using a cURL call to an external source. 
$cnt=0;

my $xs = XML::Simple->new(ForceArray => 1, KeepRoot => 1, KeyAttr=>[]);
my $config = $xs->XMLin($rsp);

#print Dumper($config);


## SO what I want to do here is create a loop using and index to loop
# Through all possible entries in my XML data. The line below prints out
# correctly the first 'Entry' in 'Entries' of Field 16. 


$dataFromField16= "$config->{'Entries'}->[0]->{'Entry'}->[0]->{'Field16'}->[0]";

print "$dataFromField16\n";

Open in new window

0
Comment
Question by:nachtmsk
3 Comments
 
LVL 12

Expert Comment

by:mwochnick
ID: 36581853
why not use a while loop and process until you run out of records?
0
 
LVL 1

Accepted Solution

by:
rajeeshb earned 500 total points
ID: 36582545
my @table = $perl->{Entries};
my @ntable = $table[0];
my $count = @ntable;
print "Count is $count\n";
0
 

Author Closing Comment

by:nachtmsk
ID: 36584238
Perfect. Thanks!
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question