Search code examples
xmlperlxml-libxml

How to encode symbols "<" and ">" in Perl/XML


I am using Perl to create an XML file and then using

use XML::LibXML;

In one part of code I need to write some explanation in one node by

$writer->characters("<![CDATA[$xmlFile]]>");

What I have as an output is

&lt;![CDATA[example.xml]]&gt;

But what expect to have would be

<![CDATA[example.xml]]>

I have checked &lt; and &gt;, but it does not work at all. Because istead of symbol & again it prints out encoding, too.

Does anyone have an idea how to come over?


Solution

  • Don't try to use characters to write a CDATA section directly; it generates text, not elements and not special constructs like CDATA sections, PIs, or comments. Instead (assuming $writer is an XML::Writer) you can do $writer->cdata($xmlFile).