Solved

Perl and XML::Simple

Posted on 2011-09-22
3
265 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

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

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…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
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…

734 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