Here's some code that takes an associative array and prints it asXML() but creates CDATA sections for each string
<?php
class SimpleXMLExtended extends SimpleXMLElement{
public function addCData($string){
$dom = dom_import_simplexml($this);
$cdata = $dom->ownerDocument->createCDATASection($string);
$dom->appendChild($cdata);
}
}
function assocArrayToXML($root_element_name,$ar){
$xml = new SimpleXMLExtended("<?xml version=\"1.0\"?><{$root_element_name}></{$root_element_name}>");
$f = create_function('$f,$c,$a','
foreach($a as $k=>$v) {
if(is_array($v)) {
if (!is_numeric($k))$ch=$c->addChild($k);
else $ch = $c->addChild(substr($c->getName(),0,-1));
$f($f,$ch,$v);
} else {
if (is_numeric($v)){ $c->addChild($k, $v);
}else{$n = $c->addChild($k); $n->addCData($v);}
}
}');
$f($f,$xml,$ar);
return $xml->asXML();
}
/* sample */
$result = array("title"=>"CDATA Sample");
$result['items'] = array();
$result['items'][] = array('title'=>'Some string', 'number' => 1);
$result['items'][] = array('title'=>'Some string', 'number' => 2);
$result['items'][] = array('title'=>'Some string', 'number' => 3);
echo assocArrayToXML('result',$result);
?>
The is_numeric check could be changed by a more elaborate regular expression to check if the string is actually xml unsafe but this worked for me.
DOMDocument::createCDATASection
(PHP 5)
DOMDocument::createCDATASection — Crée un nouveau noeud cdata
Description
Cette fonction crée une nouvelle instance de la classe DOMCDATASection. Ce noeud ne sera pas affiché dans le document, à moins qu'il ne soit inséré avec DOMNode::appendChild().
Liste de paramètres
-
data -
Le contenu du cdata.
Valeurs de retour
Le nouveau DOMCDATASection ou FALSE
si une erreur survient.
Voir aussi
- DOMNode::appendChild() - Ajoute un nouveau fils à la fin des fils
- DOMDocument::createAttribute() - Crée un nouvel attribut
- DOMDocument::createAttributeNS() - Crée un nouvel attribut avec un espace de noms associé
- DOMDocument::createComment() - Crée un nouveau noeud de commentaire
- DOMDocument::createDocumentFragment() - Crée un nouveau fragment de document
- DOMDocument::createElement() - Crée un nouveau noeud
- DOMDocument::createElementNS() - Crée un nouveau noeud avec un espace de noms associé
- DOMDocument::createEntityReference() - Crée un nouveau noeud de référence d'entité
- DOMDocument::createProcessingInstruction() - Crée un nouveau noeud PI
- DOMDocument::createTextNode() - Crée un nouveau noeud de texte
Marc info[at]braincast.nl ¶
2 years ago
jesdisciple dot FOO at gmail dot BAR dot com ¶
2 years ago
If you would like to refer to the documentation for the class of the returned object, see http://www.php.net/manual/en/class.domcharacterdata.php
info at troptoek dot com ¶
5 years ago
A common issue seems to be adding javascript to CDATA and the browser throwing a javascript error. To ensure the javascript works use the following code when adding CDATA:
<?php
/**
* Append Caracter Data to a node and check for a javascript node
*
* @param DOMElement $appendToNode
* @param string $text
*/
function appendCdata($appendToNode, $text)
{
if (strtolower($appendToNode->nodeName) == 'script') { // Javascript hack
$cm = $appendToNode->ownerDocument->createTextNode("\n//");
$ct = $appendToNode->ownerDocument->createCDATASection("\n" . $text . "\n//");
$appendToNode->appendChild($cm);
$appendToNode->appendChild($ct);
} else { // Normal CDATA node
$ct = $appendToNode->ownerDocument->createCDATASection($text);
$appendToNode->appendChild($ct);
}
}
?>
The result should be:
<script type="text/javascript">
//<![CDATA[
function someJsText() {
document.write('Some js with <a href="#">HTML</a> content');
}
//]]></script>
