1

I am using my xml file to store special chars.

This is my original file

<root>
  <popups>
        <popup id="1">
            <text1>
                <![CDATA[dynamic text popup 2a]]>
            </text1>
            <text2>
                <![CDATA[dynamic text popup 2b]]>
            </text2>
         </popup>
     </popups>
</root>

Now when I use php to save special chars eg , it becomes like that

<root>
    <popups>
        <popup id="1">
            <text1>&lt;![CDATA[Hello world]]&gt;</text1>
            <text2>&lt;![CDATA[asassa]]&gt;</text2>
        </popup>
    </popups>
</root>

I have used the following code :

$this->xmlDocument = simplexml_load_file("xml/conf.xml");
    $pages_node = $this->xmlDocument->xpath("/root/popups/popup[@id=1]");

    $name = $_POST['popup-name'];
    $editor1 = trim(strip_tags($_POST['editor1']));
    $editor2 = trim(strip_tags($_POST['editor2']));
    if (!empty($name)){
        if (!empty($editor1)){ 
            $pages_node[0]->text1 = "<![CDATA[".$editor1."]]>"; 
        }
        if (!empty($editor2)){ 
            $pages_node[0]->text2 =  "<![CDATA[".$editor2."]]>" ;
        }
        $this->xmlDocument->asXml($this->basePath() . "conf/conf.xml");
    }

How can I save the special chars as they are without needing to encode them?

1 Answer 1

3

Simplexml is meant to be simple, so there is no such option. dom_import_simplexml can help you create domdocument from simplexml object.

You have to create new instance of DOMDocument, then create CDATA section and put it into imported DOMElement node.

If you are using php DomDocument, you have to create DOMCDATASection and append it to text1/text2 nodes.

If you don't have text1 and text2 nodes, you have to create them first, then appencd cdata node to them and finally append them to popup:

$cdata = $dom->createCDATASection("test");

$text1 = $dom->createElement('text1');
$text1->appendChild($cdata);

$text1 = $dom->createElement('text2');
$text2->appendChild($cdata);

$popupNode->appendChild($text1);
$popupNode->appendChild($text2);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.