Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用perl将xml字符串放入html表格单元格_Html_Xml_Perl - Fatal编程技术网

如何使用perl将xml字符串放入html表格单元格

如何使用perl将xml字符串放入html表格单元格,html,xml,perl,Html,Xml,Perl,我有一个变量中的xml消息,希望使用表在浏览器上显示。我在下面进行了尝试,但在呈现到浏览器的过程中,实际的xml字符串没有显示出来 open FH, ">report.html"; my $x=qq(<?xml version="1.0" encoding="utf-8" ?> <Soap-ENV:Envelope xmlns:Soap-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:msdwHdr="http

我有一个变量中的xml消息,希望使用表在浏览器上显示。我在下面进行了尝试,但在呈现到浏览器的过程中,实际的xml字符串没有显示出来

open FH, ">report.html";

my $x=qq(<?xml version="1.0" encoding="utf-8" ?>
<Soap-ENV:Envelope xmlns:Soap-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:msdwHdr="http://xml.xdft.com/ns/appmw/soap/1.0/header" version="1.1">
 <Soap-ENV:Body>
  <CheckTrade>
   <tradeId>492195</tradeId>
  </CheckTrade>
 </Soap-ENV:Body>
</Soap-ENV:Envelope>);

print FH "<table><tr><td>test</td><td>$x</td></tr></table>";

您遇到的问题是XML文档在HTML中无法很好地发挥作用。所有的
字符都会混淆您的浏览器,因此您只能获得tradeID的值。在将XML字符串放入HTML文档之前,请先了解如何对其进行编码。

您遇到的问题是XML文档在HTML中无法很好地发挥作用。所有的
字符都会混淆您的浏览器,因此您只能获得tradeID的值。在将XML字符串放入HTML文档之前,请查看如何对其进行编码。

您也可以通过将其放入
[CDATA][1]
部分来转义整个XML字符串:

print FH "<table><tr><td>test</td><td><![CDATA[$x]]></td></tr></table>";
或者更清楚地说:

 $x=~ s{]]>}                   # CDATA end marker
       {]                      # beginning of the end marker
         ]]>                   # end the first CDATA section
            <![CDATA[          # start a new CDATA section
                     ]>}gx;    # end of the end marker
$x=~s{]>}#CDATA结束标记
{]#结束标记的开始
]]>#结束第一个CDATA部分
}gx;#结束标记的结束
关于Perl风格的几个注意事项:

  • 当前的最佳实践是通过使用词法文件句柄来避免裸字文件句柄

  • 还认为最好使用open的3 args形式,在您的情况下,很可能使用编码:

所以最好写下这个:

open( my $fh, '>:utf8', 'report.html') or die "cannot open reports.html: $!";
...
print {$fh} "<table><tr><td>test</td><td><![CDATA[$x]]></td></tr></table>";
open(my$fh,”>:utf8,“report.html”)或die“无法打开reports.html:$!”;
...
打印{$fh}“测试”;

您也可以通过将整个XML字符串放在
[CDATA][1]
部分来转义它:

print FH "<table><tr><td>test</td><td><![CDATA[$x]]></td></tr></table>";
或者更清楚地说:

 $x=~ s{]]>}                   # CDATA end marker
       {]                      # beginning of the end marker
         ]]>                   # end the first CDATA section
            <![CDATA[          # start a new CDATA section
                     ]>}gx;    # end of the end marker
$x=~s{]>}#CDATA结束标记
{]#结束标记的开始
]]>#结束第一个CDATA部分
}gx;#结束标记的结束
关于Perl风格的几个注意事项:

  • 当前的最佳实践是通过使用词法文件句柄来避免裸字文件句柄

  • 还认为最好使用open的3 args形式,在您的情况下,很可能使用编码:

所以最好写下这个:

open( my $fh, '>:utf8', 'report.html') or die "cannot open reports.html: $!";
...
print {$fh} "<table><tr><td>test</td><td><![CDATA[$x]]></td></tr></table>";
open(my$fh,”>:utf8,“report.html”)或die“无法打开reports.html:$!”;
...
打印{$fh}“测试”;

很好,谢谢,使用HTML::Entities;my$encoded=encode_实体($xml);工作良好,谢谢,使用HTML::Entities;my$encoded=encode_实体($xml);谢谢,我会遵守规则的。谢谢,我会遵守规则的。