Skip to content

Instantly share code, notes, and snippets.

@nullthoughts
Created August 11, 2018 17:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nullthoughts/718f4d81da204edd41de16681e5c431c to your computer and use it in GitHub Desktop.
Save nullthoughts/718f4d81da204edd41de16681e5c431c to your computer and use it in GitHub Desktop.
Extends SimpleXMLElement to support writing CDATA
<?php
class SimpleXMLExtended extends SimpleXMLElement
{
// Inspired by: https://stackoverflow.com/questions/6260224/how-to-write-cdata-using-simplexmlelement/6260295
public function addCDATA($string)
{
$node = dom_import_simplexml($this);
$nodeOwner = $node->ownerDocument;
$node->appendChild($nodeOwner->createCDATASection($string));
}
public function appendChildWithCDATA($key, $value)
{
$this->addChild($key);
$this->{$key}->addCData($value);
}
}
@vicgonvt
Copy link

Clean

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment