How can I escape text for an XML document in Perl?

前端 未结 9 1169
长发绾君心
长发绾君心 2021-01-01 06:53

Anyone know of any Perl module to escape text in an XML document?

I\'m generating XML which will contain text that was entered by the user. I want to correctly handl

9条回答
  •  梦毁少年i
    2021-01-01 07:10

    Although you better use a module like XML::LibXML or XML::Code you could wrap textual data in a CDATA section. You must only take care not to put ]]> in it (this sequence is also disallowed outside of CDATA sections!):

    $text =~ s/\]\]>/]]>]]>";
    $xml = "$text"; 
    

    As bonus your code will look more perlish obfuscated! :-)

提交回复
热议问题