http://qs1969.pair.com?node_id=601018


in reply to Print out an XML file to browser

You should be able to just open foo.xml in your browser by clicking on the file. But if you'd like to output XML from your script to your browser and view it as XML, I think you need to change the Content-Type. I checked the CGI documentation, and it looks like this is how you'd do it:

print start_html(-head=>meta({-http_equiv => 'Content-Type', -content => 'text/xml'}))

Someone please correct me if I am wrong.

Replies are listed 'Best First'.
Re^2: Print out an XML file to browser
by lakeTrout (Scribe) on Feb 20, 2007 at 04:37 UTC
    Hi been42

    That is exactly what I am looking to do. I tried your start_html but it just returns a time stamp to the browser. Am I missing something more fundemental? I'm a bit of a CGI novie, but here is what I have so far:

    optput:Mon Feb 19 21:34:25 2007 processremote.cgi: readline() on closed filehandle XMLFILE at /my/upload/path/processremote.cgi line 15.

    #!/usr/bin/perl use strict; use warnings; use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser); print header; print start_html(-head=>meta({-http_equiv => 'Content-Type', -content => 'text/xml'})); open(XMLFILE, "/my/upload/path/master.xml"); while (<XMLFILE>){ print $_; }
      Two things:
      1. If you know you want to output xml, then print header('text/xml'); is a lot simpler.
      2. Always check to see if your 'open' succeeded. Your error message means that your script didn't open the file.
      #!/usr/local/bin/perl use strict; use warnings; use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser); print header('text/xml'); my $file = "/some/file.xml"; open(XMLFILE, $file) or die "Failed to open $file, $!"; while (<XMLFILE>){ print; }
        Thanks Virtualse

        The above code seems to work but I get a bad headder error. It "works" mening in prints by hand, just not to the browser. What might cause that?
        Thanks for the correction. I thought I remembered that it was something simple, but I guess I just skipped over that in the docs when I looked.
      Please make sure whether you have "master.xml" in the specified location.
      Use the below code.
      open(XMLFILE, "/my/upload/path/master.xml") or die("Can't open file m +aster.xml : $!");
      also its best practice to close the filehandle after the usage.
      close(XMLFILE)
        It isn't really necessary to bother about closing a read-only filehandle. If you want to be rigorous and check everything, then why aren't you checking whether the print statements succeed? :) If you were writing to the file, then I agree - close the filehandle and check to see if an error occurred.
        how does the <while> look?
Re^2: Print out an XML file to browser
by dorward (Curate) on Feb 20, 2007 at 11:50 UTC

    You need a real HTTP header, http-equiv is:

    • only applicable to HTML documents
    • can't specify content-type in a way that anything other than Russian Apache will care about

    The CGI.pm docs explain how to create standard http headers.