Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: Print out an XML file to browser

by been42 (Curate)
on Feb 20, 2007 at 04:10 UTC ( #601018=note: print w/replies, xml ) Need Help??

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.
        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 docs explain how to create standard http headers.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://601018]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2022-12-10 04:54 GMT
Find Nodes?
    Voting Booth?

    No recent polls found