Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Print out an XML file to browser

by odrm (Novice)
on Feb 20, 2007 at 12:06 UTC ( [id://601076] : note . print w/replies, xml ) Need Help??


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!)

Replies are listed 'Best First'.
Re^2: Print out an XML file to browser
by lakeTrout (Scribe) on Feb 20, 2007 at 19:26 UTC
    Perfect! Thank you all, I have it working!
Re: Print out an XML file to browser
by benizi (Hermit) on Feb 20, 2007 at 21:13 UTC

    Nice. I like your coding style, odrm. But, it looks like you've got a copy-paste error in part 2. (s/</&gt;/g should be s/>/&gt;/g, yes?)

      Well spotted - yes it should be. I know why I prefer having perl do my escaping, over doing it by hand!