bimleshsharma has asked for the wisdom of the Perl Monks concerning the following question:

I have xml message in variable and i want to put in html->table->cell but i am not sure why that xml string is not displaying on web browser. here is code:

open FH, ">tt.html" or die "file could not be not created"; my $x= qq(<?xml version="1.0" encoding="utf-8" ?> <Soap-ENV:Envelope> <Soap-ENV:Body> <CheckTrade> <tradeId>49214792</tradeId> </CheckTrade> </Soap-ENV:Body> </Soap-ENV:Envelope> ); print FH "<table><tr><td>Message</td><td>$x</td></tr></table>;
If i run above and see the html in browser then it looks like as below. Where i can see first TD value but not not second TD value exactly. Instead of this ID it should show complete $x. I have used print command for $x and value is coming fine. Please help Thanks
________________________ Message | 49214792 __________|_____________

Replies are listed 'Best First'.
Re: how to put XML string in table cell
by Corion (Patriarch) on Jan 22, 2014 at 10:08 UTC

    What is the real HTML output of your script?

    Also, if you want to print the XML that is contained in $x, then you maybe want to learn about how HTML encodes <>.

    I suggest HTML::Entities:

    print encode_entities( $x );

      i analysed the html code and found that things are coming right..

      <table><tr><td>1</td><td>"Soap Msg Comp"</td><td>49214792</td><td><?xm +l version="1.0" encoding="utf-8" ?> <Soap-ENV:Envelope version="1.1"> <Soap-ENV:Body> <CheckTrade> <tradeId>4921492</tradeId> </CheckTrade> </Soap-ENV:Body> </Soap-ENV:Envelope></td></tr></table>

        No, they are not coming right. <Soap-ENV: is not a valid HTML tag.

Re: how to put XML string in table cell
by tangent (Parson) on Jan 22, 2014 at 13:37 UTC
    You need to encode the XML tags for the browser to display them - see here for an explanation. It is quite simple to do this using the module HTML::Entities. Note also the safer 3 argument form of open()
    use HTML::Entities; open (my $fh, '>', 'tt.html') or die "Could not open tt.html, $!"; my $x= qq(<?xml version="1.0" encoding="utf-8" ?> <Soap-ENV:Envelope> <Soap-ENV:Body> <CheckTrade> <tradeId>49214792</tradeId> </CheckTrade> </Soap-ENV:Body> </Soap-ENV:Envelope> ); encode_entities($x); print $fh "<table><tr><td>Message</td><td>$x</td></tr></table>";