note 102819 added to domdocument.createcdatasection

Marcinfo
Newsgroups php.notes
Message-ID <[email protected]>
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.
----
Server IP: 69.147.83.197
Probable Submitter: 213.93.196.40
----
Manual Page -- http://www.php.net/manual/en/domdocument.createcdatasection.php
Edit        -- https://master.php.net/note/edit/102819
Del: integrated  -- https://master.php.net/note/delete/102819/integrated
Del: useless     -- https://master.php.net/note/delete/102819/useless
Del: bad code    -- https://master.php.net/note/delete/102819/bad+code
Del: spam        -- https://master.php.net/note/delete/102819/spam
Del: non-english -- https://master.php.net/note/delete/102819/non-english
Del: in docs     -- https://master.php.net/note/delete/102819/in+docs
Del: other reasons-- https://master.php.net/note/delete/102819
Reject      -- https://master.php.net/note/reject/102819
Search      -- https://master.php.net/manage/user-notes.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.