Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: Print out an XML file to browser

by odrm (Novice)
on Feb 20, 2007 at 12:06 UTC ( #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!

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2022-10-04 16:52 GMT
Find Nodes?
    Voting Booth?
    My preferred way to holiday/vacation is:

    Results (18 votes). Check out past polls.