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

In reply to Managing data after XML::Simple...count elements by inblosam

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.