I have this code to create and update xml file:
\');
$xml->title
Here is my combined solution with adding child with CDATA or adding CDATA to the node.
class SimpleXMLElementExtended extends SimpleXMLElement
{
/**
* Add value as CData to a given XML node
*
* @param SimpleXMLElement $node SimpleXMLElement object representing the child XML node
* @param string $value A text to add as CData
* @return void
*/
private function addCDataToNode(SimpleXMLElement $node, $value = '')
{
if ($domElement = dom_import_simplexml($node))
{
$domOwner = $domElement->ownerDocument;
$domElement->appendChild($domOwner->createCDATASection("{$value}"));
}
}
/**
* Add child node with value as CData
*
* @param string $name The child XML node name to add
* @param string $value A text to add as CData
* @return SimpleXMLElement
*/
public function addChildWithCData($name = '', $value = '')
{
$newChild = parent::addChild($name);
if ($value) $this->addCDataToNode($newChild, "{$value}");
return $newChild;
}
/**
* Add value as CData to the current XML node
*
* @param string $value A text to add as CData
* @return void
*/
public function addCData($value = '')
{
$this->addCDataToNode($this, "{$value}");
}
}
// Usage example:
$xml_doc = '
';
$xml = new SimpleXMLElementExtended($xml_doc);
$offer = $xml->addChild('o');
$offer->addAttribute('id', $product->product_id);
$offer->addAttribute('url', 'some url');
$cat = $offer->addChildWithCData('cat', 'Category description as CDATA');
// or
$cat = $offer->addChild('cat');
$cat->addCData('Category description as CDATA');