note 86085 deleted from simplexmlelement.addChild by danbrown
| From: | danbrown@php.net | Date: | Fri, 28 Jan 2011 14:34:28 +0000 |
| Subject: | note 86085 deleted from simplexmlelement.addChild by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-176460@lists.php.net to get a copy of this message | ||
Note Submitter: Donet
----
I found this easy way to create a CDATA section as child value:
I extend the class SimpleXMLElement:
<?php
class SimpleXMLExtended extends SimpleXMLElement
{
public function addCData($cdata_text)
{
$node= dom_import_simplexml($this);
$no = $node->ownerDocument;
$node->appendChild($no->createCDATASection($cdata_text));
}
}
?>
Then in the main code:
<?php
$xml = new SimpleXMLExtended('<?xml version = "1.0" encoding =
"UTF-8"?><root></root>');
$mynode = $xml->addChild("myname");
$mynode->addCData("my name contains everything I want &%<>");
echo $xml->asXml();
?>