Saturday, 27 December 2014

Append a CDATA with XML::LibXML appendTextNode() AS-IS



I use this code to create a new node with the expected output:



<item desc="desc foobar"><![CDATA[qux]]></item>


the code :



open my $fh, "<", $xml_file;
binmode $fh;
my $parser = XML::LibXML->new();
my $doc = $parser->load_xml(IO => $fh);

# create a new node in XML file
my $root = $doc->getDocumentElement();
my $new_element = $doc->createElement("item");
# FIXME
$new_element->appendTextNode(sprintf '<![CDATA[%s]]>', join "\n", @input);
$new_element->setAttribute('desc', $desc);
$root->appendChild($new_element);

close $fh;

open my $out, '>', $xml_file;
binmode $out;
$doc->toFH($out);

close $out;


it work well to create new elements texts, but I wonder how to add CDATA without the XML entities substitution : I get :



<item desc="dddd">&lt;![CDATA[qux]]>
# ^^^^

No comments:

Post a Comment