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


in reply to Print out an XML file to browser

Here are two options:

  1. send the xml to the browser, and let the browser render it:
    use CGI qw(:cgi); # don't need HTML, only cgi stuff
    print header(-type => 'application/xml');  # make browser expect xml
    open XML, '<', 'foo.xml' or die $!;
    print while <XML>;
    
  2. print an HTML rendering of your xml file:
    use CGI qw(:standard);
    sub escape_xml ($) {
      my $text = shift;
      $text =~ s/</&lt;/g;
      $text =~ s/ < >/&gt;/g;
      return $text;
    }
    print header, start_html('XML File');
    open XML, '<', 'foo.xml' or die $!;
    print "<pre>\n";
    print escape_xml($_) while <XML>;
    print "\n</pre>";
    
updated to fix the typo spotted by benizi (Thank you!)