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

Hi Monks!
This code is the best I found to represent what I am trying to do. I am trying to save the results of this code into a XML file so I can use in another script, I tried using "Data::Dumper" but the structure saved is complicated to use, it saves the file in 15 chunks of data like:
$VAR1 = { 'xml_api_reply' => { '-version' => '1', 'weather' => { .... } $VAR1 = { 'xml_api_reply' => { '-version' => '1', 'weather' => { .... } ....

One block of data for each zip code (15).
Is there any better way to save this result in a file as one big XML?
Here is the code I am using to try this example:
#!/usr/bin/perl -w use strict; use CGI qw(-oldstyle_urls :standard); use XML::TreePP; use XML::XPath; use XML::Simple; use Data::Dumper; use Date::Format; use Date::Parse; my $q = new CGI; $| = 1; my @zipcode = qw(02151 01908 02638 02669 02670 02536 02642 02574 02332 + 02601 02045 01930 02152 27943 02169); my ($currtempf, $currtempc, $currhumidity, $currcondition, $currwind, +$todayhigh, $todaylow, $todaycond, $tomorrowcond); my ($city, $url, $tpp, $tree); #set $Data::Dumper::Purity to 1 if you have nested references $Data::Dumper::Purity = 1; open (FILE, "> data.txt") or die "can't open $tree: $!"; foreach my $zip(@zipcode) { next unless $zip =~ /^(\d{5})$/; $url = "http://www.google.com/ig/api?weather=".$zip ; $tpp = XML::TreePP->new(); $tree = $tpp->parsehttp( GET => $url ); print FILE Dumper($tree); } close FILE or die "can't close $tree: $!";

Thanks for the help!!!

Replies are listed 'Best First'.
Re: Save results into one big XML file help!
by BrowserUk (Patriarch) on Aug 16, 2012 at 03:17 UTC
    the structure saved is complicated to use, it saves the file in 15 chunks of data like: ...

    You presumably used print Dumper( @array ) which resulted in the 15 chunks.

    Had you done print Dumper( \@array ); you'd find the results much easier to work with.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      How could I use that there, I have the variable $tree to use not @tree, right?

        my @data; foreach my $zip(@zipcode) { ... $tree = $tpp->parsehttp( GET => $url ); push @data, $tree; } print FILE Dumper \@data;

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?

Re: Save results into one big XML file help!
by cheekuperl (Monk) on Aug 16, 2012 at 02:58 UTC
    I am wondering why you are not using old school method of writing the data to file using a few print statements of your own!
    This may cost you a few extra lines of code, but you will be able to write the file the way you wish!
    Sorry if this sounds too naive, or doesn't fit your requirement.

      cheekuperl:

      ...and chances are, it would be wrong. XML looks much simpler than it actually is.

      I spend most of my @work time working with data interchange with various financial institutions and get to work with 'XML' files all the time. The problem is, they tend to generate it just as you say: with a few print statements. So what happens is we get files that *look* like they should be XML, but fail in one or more ways.

      This is one of the reasons you see websites and databases with text like "<amp;" and such everywhere. Someone doesn't encode properly, someone doesn't decode properly, etc.

      The problem is so common that whenever I get a new XML data feed, the first thing I do is run it through an XML validator. (Of course, when you point out that the file you receive isn't valid XML, they'll adamantly insist that it is. Then you're stuck with the job of trying to get the data into your system without making the data any worse than it already is.)

      For all the problems that XML is supposed to solve, I have more headaches with XML feeds than anything else.

      ...roboticus

      When your only tool is XML, all data interchange problems look like a pain in the ass.

Re: Save results into one big XML file help!
by philiprbrenan (Monk) on Aug 29, 2012 at 21:52 UTC

    If the other script is Perl, have you considered using Storable?