inblosam has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to get this XML parsing stuff figured out. I am using XML::Simple which has turned out to be...simple. My problem now is with handling the data afterwards. I need to know how many sets of <Table> data are in there, so I can cycle through them. I can't figure out how to get a count though!
#!/usr/bin/perl -w use strict; use XML::Simple; my $xml = qq|<?xml version="1.0" encoding="utf-8"?> <NewDataSet> <Table> <CITY>Provo</CITY> <STATE>UT</STATE> <ZIP>84601</ZIP> <AREA_CODE>801</AREA_CODE> <TIME_ZONE>M</TIME_ZONE> </Table> <Table> <CITY>Provo</CITY> <STATE>UT</STATE> <ZIP>84605</ZIP> <AREA_CODE>801</AREA_CODE> <TIME_ZONE>M</TIME_ZONE> </Table> <Table> <CITY>Provo</CITY> <STATE>SD</STATE> <ZIP>57774</ZIP> <AREA_CODE>605</AREA_CODE> <TIME_ZONE>C</TIME_ZONE> </Table> <Table> <CITY>Provo</CITY> <STATE>KY</STATE> <ZIP>42267</ZIP> <AREA_CODE>502</AREA_CODE> <TIME_ZONE>E</TIME_ZONE> </Table> </NewDataSet>|; my $perl = XMLin($xml); use Data::Dumper; my @table = $perl->{Table}; my @newtable = $table[0]; my $count = @newtable; print Dumper(\@newtable); print "Count is $count\n"; print "State is $newtable[0][3]{STATE}\n";
Of course I will be putting in some dynamic XML when I get this to work, so I need to know how many elements were returned so I can get city, state, and zip information out for each entry. Here are the results of the above code, and obviously the count is 1 because that is how many arrays there are. I am sure it is not overly complicated, I have just tried and tried to no avail.
$VAR1 = [ [ { 'STATE' => 'UT', 'ZIP' => '84601', 'AREA_CODE' => '801', 'TIME_ZONE' => 'M', 'CITY' => 'Provo' }, { 'STATE' => 'UT', 'ZIP' => '84605', 'AREA_CODE' => '801', 'TIME_ZONE' => 'M', 'CITY' => 'Provo' }, { 'STATE' => 'SD', 'ZIP' => '57774', 'AREA_CODE' => '605', 'TIME_ZONE' => 'C', 'CITY' => 'Provo' }, { 'STATE' => 'KY', 'ZIP' => '42267', 'AREA_CODE' => '502', 'TIME_ZONE' => 'E', 'CITY' => 'Provo' } ] ]; Count is 1 State is KY

Replies are listed 'Best First'.
Re: Managing data after XML::Simple...count elements
by japhy (Canon) on Jul 02, 2004 at 16:07 UTC
    You have:
    my @table = $perl->{Table}; my @newtable = $table[0]; my $count = @newtable;
    But $perl->{Table} returns an array reference, not an array. Instead, do:
    my @table = @{ $perl->{Table} }; my $count = @table;
    Now $count should be accurate, and @table should hold hash references (I think).
    _____________________________________________________
    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;
      Thanks!! That was what I needed!! I don't think I would have been able to figure that out on my own. :)
Re: Managing data after XML::Simple...count elements
by grantm (Parson) on Jul 05, 2004 at 06:20 UTC
    my $perl = XMLin($xml);

    Calling XMLin() without specifying any options is often a bug waiting to happen, see this node.

    Also, when looking for meaningful variable names, it might help to bear in mind that XMLin will throw away the top-level XML element name by default - why not use that:

    my $NewDataSet = XMLin($xml, KeyAttr => [], ForceArray => ['Table']) +;

    Then when you index into the data, the Perl symbols and the XML element names correspond naturally:

    my $table = $NewDataSet->{Table}; my $table_count = @$table;
      Fantastic. Funny enough I just ran into the problem that when there was only one result returned, it would give me an error because there was no array. Now with these options it works all around. WOW! THANKS!