0

I am trying to display an XML content (which is a SOAP request) in a simple HTML table using CGI Perl. However, the XML content is getting truncated inside the table cell. Can anybody suggest a solution to render the XML properly in the HTML page?

Below is the code :

use strict;

use CGI;
use CGI::Carp qw(fatalsToBrowser);

my $qry = new CGI;

&show_html();


sub show_html {
    $qry->header();
    $qry->start_html(-bgcolor=>'#FFFFFF', -title=>'Rendering XML');
    my $body = &display_page();
    print $body;

    $qry->end_html;
}

sub display_page {

    my $html = qq{
        <table border="1">
        <tr>
            <th> Key </th>
            <th>Value</th>
        </tr>
        <tr>
            <td> SOAP </td>
            <td>POST /Quotation HTTP/1.0 Host: www.demohost Content-Type: text/xml; charset=utf-8 Content-Length: nnn  <?xml version="1.0"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.demourl1/2001/12/soap-envelope" SOAP-ENV:encodingStyle="http://www.demourl2/2001/12/soap-encoding" >     <SOAP-ENV:Body xmlns:m="http://www.demourl3/quotations" >              <m:GetQuotation>          <m:QuotationsName>MiscroSoft</m:QuotationsName>       </m:GetQuotation>                  </SOAP-ENV:Body>      </SOAP-ENV:Envelope>
            </td>
        </tr>
        </table>
    };
    return $html;

}
1
  • Please don't use an ampersand & when calling Perl subroutines. That hasn't been correct since Perl v5.5 was released seventeen years ago Commented Dec 2, 2015 at 12:15

1 Answer 1

2

XML is full of characters like < and & which has special meaning in HTML.

You need to encode them.

That is typically done using the HTML::Entities module.

use HTML::Entities;
my $html_encoded_string = encode_entities($raw_xml_string);

Alternatively, use a template language which supports HTML filtering.

In Template-Toolkit, for instance, you would do something along the lines of:

<td>[% raw_xml_string | html %]</td>

You could also use CGI.pm's HTML generating functions (as you are doing in show_html but they are marked HTML Generation functions should no longer be used so you really should stop using them in show_html.

The CGI.pm documentation recommends the Template approach.

Sign up to request clarification or add additional context in comments.

1 Comment

This is partially working, still few characters are spilling out of that <td></td> element here and there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.