[英]How to render xml content in a HTML table using CGI Perl
我正在嘗試使用CGI Perl在簡單的HTML表中顯示XML內容(這是SOAP請求)。 但是,XML內容在表單元格內被截斷。 有人可以提出解決方案以在HTML頁面中正確呈現XML嗎?
下面是代碼:
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;
}
XML充滿了<
和&
類的字符,在HTML中具有特殊含義。
您需要對它們進行編碼。
通常使用HTML::Entities
模塊完成此操作 。
use HTML::Entities;
my $html_encoded_string = encode_entities($raw_xml_string);
或者,使用支持HTML篩選的模板語言。
例如,在Template-Toolkit中,您將按照以下方式進行操作:
<td>[% raw_xml_string | html %]</td>
您還可以使用CGI.pm的HTML生成函數(就像在show_html
中所做的show_html
但是它們被標記為HTML生成函數,因此不再使用,因此您實際上應該在show_html
停止使用它們。
CGI.pm文檔建議使用“模板”方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.