note 86085 modified in function.simplexml-element-addChild by danbrown
| From: | danbrown@php.net | Date: | Wed, 16 Sep 2009 16:38:07 +0000 |
| Subject: | note 86085 modified in function.simplexml-element-addChild by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-160737@lists.php.net to get a copy of this message | ||
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();
?>
--was--
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();
?>
http://php.net/manual/en/function.simplexml-element-addChild.php