note 86085 modified in function.simplexml-element-addChild by danbrown

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
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:

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:
$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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.