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

时间:2022-08-17 08:58:12

I have xml message in a variable and want to display on browser using table. I tried below but during rendering to browser the actual xml string is not showing.

我有一个变量中的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>";

If i run above code and open report.html in browser i am able to see first TD value and in second TD it is showing only 492195 (that is part of tradeid) i want to see complete $x value. I looked at source of html.It is showing same as $x but on browser it is not.

如果我运行上面的代码和打开的报告。在浏览器中,我可以看到第一个TD值,在第二个TD中,它只显示492195(这是tradeid的一部分),我想看到完整的$x值。我查看了html的源代码。它显示与$x相同,但在浏览器上不是。

______________
test  | 492195 
______|________ 

2 个解决方案

#1


2  

The issue that you're having is the XML document doesn't play nicely in HTML. All the <> characters confuse your browser and thus you're only getting the value of tradeID. Look at this answer for how to encode your XML string before you put it into the HTML document.

您遇到的问题是XML文档在HTML中不能正常工作。所有的<>字符都混淆了您的浏览器,因此您只能得到tradeID的值。在将XML字符串放入HTML文档之前,请查看这个答案。

#2


2  

You could also escape the entire XML string by putting it in a [CDATA][1] section:

您还可以将整个XML字符串转义为[CDATA][1]节:

print FH "<table><tr><td>test</td><td><![CDATA[$x]]></td></tr></table>";

Of course this works only as long as the SOAP message doesn't itself contain CDATA. If that happens, you will need to break it up: replace the CDATA end marker, ]]>, in the SOAP message by ]]]><![CDATA[]> (closing the CDATA section in the middle of the marker and starting a new one, that starts with the end of the marker:

当然,这只在SOAP消息本身不包含CDATA时才有效。如果发生这种情况,您将需要将其分解:替换CDATA结束标记,]>,在SOAP消息中by]] []>]>(在标记中间关闭CDATA部分,开始一个新的标记,从标记结束开始:</p>

$x=~ s{]]>}{]]]><![CDATA[]>}g;

or more clearly:

或更清楚:

 $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

A couple of notes on your Perl style:

关于Perl风格的一些注意事项:

  • the current best practices are to avoid bareword filehandles, by using lexical ones,

    目前的最佳实践是通过使用词汇句柄来避免光字文件句柄,

  • it's also considered better to use the 3 args form of open, in your case likely with an encoding:

    也可以考虑使用3个args格式的open,在您的情况下,可能有一个编码:

so it's probably better to write this:

所以最好这样写:

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>";

#1


2  

The issue that you're having is the XML document doesn't play nicely in HTML. All the <> characters confuse your browser and thus you're only getting the value of tradeID. Look at this answer for how to encode your XML string before you put it into the HTML document.

您遇到的问题是XML文档在HTML中不能正常工作。所有的<>字符都混淆了您的浏览器,因此您只能得到tradeID的值。在将XML字符串放入HTML文档之前,请查看这个答案。

#2


2  

You could also escape the entire XML string by putting it in a [CDATA][1] section:

您还可以将整个XML字符串转义为[CDATA][1]节:

print FH "<table><tr><td>test</td><td><![CDATA[$x]]></td></tr></table>";

Of course this works only as long as the SOAP message doesn't itself contain CDATA. If that happens, you will need to break it up: replace the CDATA end marker, ]]>, in the SOAP message by ]]]><![CDATA[]> (closing the CDATA section in the middle of the marker and starting a new one, that starts with the end of the marker:

当然,这只在SOAP消息本身不包含CDATA时才有效。如果发生这种情况,您将需要将其分解:替换CDATA结束标记,]>,在SOAP消息中by]] []>]>(在标记中间关闭CDATA部分,开始一个新的标记,从标记结束开始:</p>

$x=~ s{]]>}{]]]><![CDATA[]>}g;

or more clearly:

或更清楚:

 $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

A couple of notes on your Perl style:

关于Perl风格的一些注意事项:

  • the current best practices are to avoid bareword filehandles, by using lexical ones,

    目前的最佳实践是通过使用词汇句柄来避免光字文件句柄,

  • it's also considered better to use the 3 args form of open, in your case likely with an encoding:

    也可以考虑使用3个args格式的open,在您的情况下,可能有一个编码:

so it's probably better to write this:

所以最好这样写:

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>";