Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
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!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-04-26 06:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found