php - CakePHP Xml utility library triggers DOMDocument warning -


i'm generating xml in view cakephp's xml core library:

$xml = xml::build($data, array('return' => 'domdocument')); echo $xml->savexml(); 

view fed controller array:

$this->set(     array(         'data' => array(             'root' => array(                 array(                     '@id' => 'a & b: ok',                     'name' => 'c & d: ok',                     'sub1' => array(                         '@id' => 'e & f: ok',                         'name' => 'g & h: ok',                         'sub2' => array(                             array(                                 '@id' => 'i & j: ok',                                 'name' => 'k & l: ok',                                 'sub3' => array(                                     '@id' => 'm & n: ok',                                     'name' => 'o & p: ok',                                     'sub4' => array(                                         '@id' => 'q & r: ok',                                         '@'   => 's & t: error',                                     ),                                 ),                             ),                         ),                     ),                 ),             ),         ),     ) ); 

for whatever reason, cakephp issuing internal call this:

$dom = new domdocument; $key = 'sub4'; $childvalue = 's & t: error'; $dom->createelement($key, $childvalue); 

... triggers php warning:

warning (2): domdocument::createelement(): unterminated entity reference               t [core\cake\utility\xml.php, line 292 

... because (as documented), domdocument::createelement not escape values. however, in nodes, test case illustrates.

am doing wrong or hit bug in cakephp?

this might bug in phps domdocument::createelement() method. can avoid it. create textnode separately , append element node.

$dom = new domdocument; $dom   ->appendchild($dom->createelement('element'))   ->appendchild($dom->createtextnode('s & t: error'));  var_dump($dom->savexml()); 

output: https://eval.in/134277

string(58) "<?xml version="1.0"?> <element>s &amp; t: error</element> " 

this intended way add text nodes dom. create node (element, text , cdata, ...) , append parent node. can add more 1 node , different kind of nodes 1 parent. in following example:

$dom = new domdocument; $p = $dom->appendchild($dom->createelement('p')); $p->appendchild($dom->createtextnode('hello ')); $b = $p->appendchild($dom->createelement('b')); $b->appendchild($dom->createtextnode('world!'));  echo $dom->savexml(); 

output:

<?xml version="1.0"?> <p>hello <b>world!</b></p> 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -