tags:

views:

190

answers:

1

I am trying to put an html string inside of xml with php like this:

<?php
$xml_resource = new SimpleXMLElement('stuff.xml', 0, true);
$xml_resource->content = '<![CDATA[<u>111111111111111111111111111111111 text</u>]]>';
$xml_resource->asXML('stuff.xml');
?>

but for some reason my xml file looks like this:

<?xml version="1.0"?> <data>
    <content id="pic1" frame="1" xpos="22" ypos="22" width="11" height="11">&lt;![CDATA[&lt;u&gt;111111111111111111111111111111111 text&lt;/u&gt;]]&gt;</content> </data>

Thank you very much for your help good sirs.

+1  A: 

SimpleXML cannot create CDATA sections. However, simply assigning the HTML to a node should be functionnally equivalent:

$xml_resource->content = '<u>111111111111111111111111111111111 text</u>';

Of course the special characters will be escaped, and the result will be equivalent to using a CDATA section.


If you absolutely want to create CDATA sections, you will have to use something like SimpleDOM to access the corresponding DOM method.

include 'SimpleDOM.php';

$xml_resource = new SimpleDOM('stuff.xml', 0, true);
$xml_resource->content = '';
$xml_resource->content->insertCDATA('<u>111111111111111111111111111111111 text</u>');
$xml_resource->asXML('stuff.xml');
Josh Davis