[Catalyst] Generating a large XML document (KML file) from a database

Francisco Obispo fobispo at isc.org
Mon Nov 26 17:54:15 GMT 2012


Ok, Now that I'm on my computer, I can write a decent response:

I had a similar problem with generating a fairly large CSV file.

you don't want to store this in a stash entry because it can get very large, and 
even it you were using a temp file to write the data to, the user
would have to wait until the file is fully written and then
transferred, which could translate to unresponsiveness.

I decided to use catalyst ->write() method to output directly
to the socket, and generate the CSV on the fly.

On your example, you could use XML::Writer which allows you to 
generate XML on the fly via:

  $c->write( $writer->startTag('hello',(attribute=>'value')) );
  $c->write( $writer->characters('World!') );
  $c->write( $writer->endTag()) ;


Will generate to the output buffer:

  <hello attribute='value'>World!</hello>

You will need to generate a header with the right content type before you
start sending out stuff to your output buffer:

 $c->response->content_type('text/comma-separated-values');

 $c->res->header( 'Content-Disposition',
                     qq[attachment; filename=download.csv] );


This is how I generated mine for the CSV, you would have to fill the 
right content_type: application/xxx?

And the name of the file in the Content-Disposition section.

Notice that you will not provide a Content-Length header, so the
client will not know how much data you will be sending until its 
done.

And catalyst won't know that you actually sent anything so to avoid
Catalyst rendering the template, just set the body to something, in
my case I did:

    $c->response->body(qq{\n});

I know there are some methods to tell Catalyst to end instead
of generating the template, but I couldn't get them to work properly.


Regards


Francisco Obispo 
Director of Applications and Services - ISC
email: fobispo at isc.org
Phone: +1 650 423 1374 || INOC-DBA *3557* NOC
PGP KeyID = B38DB1BE

On Nov 26, 2012, at 8:58 AM, Robert Rothenberg <robrwo at gmail.com> wrote:

> I need to output a large XML file (actually, a KML file of points) from a
> database.
> 
> I can't find any documentation for Catalyst::View::XML::Generator, or even
> decent documentation for XML::Generator, on how to do this.
> 
> It would appear to expect me to put the entire structure in the stash, which
> I don't want to do, as it could contain thousands of points.
> 
> Ideally I would pass an iterator function and it would do the rest.
> 
> 
> 
> 
> 
> 
> _______________________________________________
> List: Catalyst at lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/




More information about the Catalyst mailing list