I am trying to generate xml data that will be read by some flash applications and it has to be in a specific format. Here is a sample of what the output should look like.
<chart caption='Monthly Sales Summary' subcaption='For the year 2006' +xAxisName='Month' yAxisName='Sales' numberPrefix='$'> <set label='January' value='17400' /> <set label='February' value='19800' /> <set label='March' value='21800' /> <set label='April' value='23800' /> <set label='May' value='29600' /> <set label='June' value='27600' /> <set label='July' value='31800' /> <set label='August' value='39700' /> <set label='September' value='37800' /> <set label='October' value='21900' /> <set label='November' value='32900' /> <set label='December' value='39800' /> </chart>
I have been able to successfully get some XML generated. For instance I can do the first line properly, but the problem comes with the loop. It's either I cannot get the syntax right, or it prints the output incorrectly.
my %xml_hash; print $xml->chart({caption => 'Chart Title', subcaption => 'Subtit +le', xAxisName => 'xAxis', yAxisName => 'yAxis'}); while ( my $row = $sp->fetchrow_hashref ) { push @{ $xml_hash{set} }, $row; } my $xmlOut = XMLout(\%xml_hash, NoAttr => 1, RootName => 'chart'); print $xmlOut;
If run this code, it prints output like this
<chart caption="Chart Title" xAxisName="xAxis" yAxisName="yAxis" subcaption="Subtitle" /><chart> <set> <label>January</label> <value>10622</value> </set> .. ... </chart>
If I try and put a foreach loop inside the creation of the XML, it gives me syntax errors
print $xml->chart({caption => 'Chart Title', subcaption => 'Subtitle', + xAxisName => 'xAxis', yAxisName => 'yAxis'}, foreach my $key ( sort keys %hash) { my ( $date, $cust ) = split( /:/, $key ); $xml->data({set => },['set'], $hash{$key}[2]); });
Any ideas on how to proceed? Thanks!

In reply to XML::Generator loop by hallikpapa

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.