- dom_import_simplexml: documentation ( source)
<?php
class SimpleXMLExtended extends SimpleXMLElement {
public function addCData($cdata_text) {
$node = dom_import_simplexml($this);
$no = $node->ownerDocument;
$node->appendChild($no->createCDATASection($cdata_text));
}
}
$oXML = new SimpleXMLExtended('<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0" />');
$oChannel = $oXML->addChild('channel');
$oChannel->addChild('title', 'TEST');
$oProduct = $oChannel->addChild('item');
$oProduct->addChild('condition1', 'new', 'http://base.google.com/ns/1.0');
$oProduct->addChild('condition2', 'new', 'http://base.google.com/ns/1.0');
$oProduct->product_type = null;
$oProduct->product_type->addCData('TEST - HOW TO GET NAMESPACE HERE');
$oProduct->addChild('condition3', 'new', 'http://base.google.com/ns/1.0');
echo $oXML->asXML();