hi monks,

this is my first attempt at trying to create some xml output from a cgi file...
it seems there must be an easier way than what i've done, so i'm posting my code for your comments/criticisms
my long term aim is to write a swell forecast extension for my firefox browser
thanks in advance for any replies.
#!/usr/bin/perl use strict; use warnings; use LWP::Simple qw(get); my $url = "http://www.oceanoutlook.com.au/table/perth.html"; my $source = get($url); use HTML::Tree; my $tree = HTML::Tree->new(); $tree->parse($source); my (@tds) = $tree->look_down( '_tag', 'td' ); my @headers; my @data; my $month; my $i = 0; foreach (@tds) { $i++; next if ($i < 5); # The first 4 headers we dont need next if ($i == 16); # Bad HTML... skip it next if ($i == scalar(@tds)); # The last one we dont need if (($i >= 5) && ($i <= 15)) { # These are the headers we want my $header = $_->as_text; if ($i == 5) { $month = $_->as_text; $header = "Date"; } # end-if $header = "Swell Direction" if ($header eq "Dir"); push(@headers, $header); } # end-if if ($i > 16) { # This is the data push(@data, $_->as_text); } # end-if } # end-foreach # Output to xml use XML::Writer; my $writer = new XML::Writer(); $writer->startTag("doc", class => "simple"); $writer->dataElement( 'title', "Swell Forecast"); $writer->dataElement( 'month', $month); my $count = -1; for ($i=0;$i<scalar(@data);$i++) { $count++; if ($count == 0) { $writer->startTag( "prediction", date => $data[$i]); $writer->dataElement( 'title', $data[$i]); $i++; $count++; } # end-if if ($count == 2) { $writer->startTag( "swell_height" ); $writer->dataElement( 'unit', 'meters'); } # end-if if ($count == 5) { $writer->startTag( "peak_period" ); $writer->dataElement( 'unit', 'seconds'); } # end-if if ($count == 8) { $writer->startTag( "wind" ); $writer->dataElement( 'unit', 'knots'); } # end-if $writer->dataElement( 'item', $data[$i], name => $headers[$count]); if ($count == 4) { $writer->endTag( "swell_height" ); } # end-if if ($count == 7) { $writer->endTag( "peak_period" ); } # end-if if ($count == 10) { $writer->endTag( "wind" ); } # end-if if ($count == scalar(@headers)-1) { $writer->endTag(); $count = -1; } # end-if } # end-for $writer->endTag(); $writer->end(); exit;
I do have one question:
does the xml need to be written to a file or can it be directly outputed from the cgi script? when i try to output from the cgi and view via firefox i get the premature headers error, but the same xml code pasted into an .xml file renders fine in firefox.
cheers,
reagen

In reply to creating xml from cgi by rsiedl

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.