?
Solved

Parsing XML file with Perl

Posted on 2011-05-09
2
Medium Priority
?
351 Views
Last Modified: 2012-05-11
Hello,

I am trying to parse and xml file using XML::Simple.
I am able to get to a single element but, i would like to loop on an array within that structure. Below look at the example that generates output of "Dimension". I want to loop over the [0],[1],[2] array.
what is the syntax for that and how can i do it?

$data2->{"saw:criteria"}->{"saw:columns"}->{"saw:column"}->[0]->{"saw:columnHeading"}->{"saw:caption"}->{"saw:text"}

Attached are the xml file and the perl script that i have written and used

Many thanks eighty-twenty.xml
#! /usr/bin/perl -w

        use XML::Simple ;
        use strict      ;
        use DBI         ;
        use Config      ;
        use Data::Dumper;
BEGIN {
   push @INC, "C:\\Perl\\RBc_Parsing";
}

main ();

sub main {


        #print Dumper($data);

        my $xml2  = new XML::Simple;
        my $data2 = $xml->XMLin("eighty+twenty.xml");




 print $data2->{"saw:criteria"}->{"saw:columns"}->{"saw:column"}->[0]->{"saw:columnHeading"}->{"saw:caption"}->{"saw:text"};
       
}

Open in new window

0
Comment
Question by:tamirmilo
2 Comments
 
LVL 27

Accepted Solution

by:
wilcoxon earned 2000 total points
ID: 35721365
This should work...
#! /usr/bin/perl

        use XML::Simple ;
        use strict      ;
        use warnings    ;
        use DBI         ;
        use Config      ;
        use Data::Dumper;
BEGIN {
   push @INC, "C:\\Perl\\RBc_Parsing";
}

main ();

sub main {

        #print Dumper($data);

        my $xml2  = new XML::Simple;
        my $data2 = $xml->XMLin("eighty+twenty.xml");

        die "invalid XML" unless (exists $data2->{'saw:criteria'}{'saw:columns'}{'saw:column'} and ref $data2->{'saw:criteria'}{'saw:columns'}{'saw:column'} eq 'ARRAY');
        foreach my $col (@{$data2->{'saw:criteria'}{'saw:columns'}{'saw:column'}}) {
                print $col->{"saw:columnHeading"}->{"saw:caption"}->{"saw:text"};
        }
       
}

Open in new window

0
 

Author Closing Comment

by:tamirmilo
ID: 35722125
Thank you.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Six Sigma Control Plans
Suggested Courses

840 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