CakePHP Xml实用程序库触发DOMDocument警告 [英] CakePHP Xml utility library triggers DOMDocument warning
问题描述
我使用CakePHP的 Xml核心视图生成XML库:
$ xml = Xml :: build($ data,array('return'=> domdocument));
echo $ xml-> saveXML();
视图是从控制器馈入一个数组:
$ 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& OK',
'sub3'=> array(
'@id'=>'M& N:OK',
'name'=>'O& P :OK',
'sub4'=> array(
'@id'=> 'Q& R:OK',
'@'=> 'S& T:ERROR',
),
),
),
),
),
),
),
),
)
);
无论什么原因,CakePHP发出一个内部调用,如下所示:
$ dom = new DOMDocument;
$ key ='sub4';
$ childValue ='S& T:ERROR';
$ dom-> createElement($ key,$ childValue);
...会触发PHP警告:
警告(2):DOMDocument :: createElement():未终止的实体引用T [CORE\Cake\Utility\Xml.php,第292行
...因为(如记录), DOMDocument :: createElement
不转义值。
我做错了什么或者我只是在CakePHP中遇到了一个错误?
$这个问题似乎出现在同时具有属性和值的节点中,因此需要使用 @ $ c $
c>语法:
'@ id'=> 'A& B:OK',//< - Handled as plain text
'name'=> 'C& D:OK',//< - Handled as plain text
'@'=> 'S& T:ERROR',// < - 作为原始XML处理
帮助函数:
protected function escapeXmlValue($ value){
return is_null($ value)? null:htmlspecialchars($ value,ENT_XML1,'UTF-8');
}
...并且在创建数组时手动调用它:
'@ id'=> 'A& B:OK',
'name'=> 'C& D:OK',
'@'=> $ this-> escapeXmlValue('S& T:NOW WORKS FINE'),
说如果它是错误或功能,因为文档没有提及。
I'm generating XML in a view with CakePHP's Xml core library:
$xml = Xml::build($data, array('return' => 'domdocument'));
echo $xml->saveXML();
View is fed from the controller with an 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 the reason, CakePHP is issuing an internal call like this:
$dom = new DOMDocument;
$key = 'sub4';
$childValue = 'S & T: ERROR';
$dom->createElement($key, $childValue);
... which triggers a PHP warning:
Warning (2): DOMDocument::createElement(): unterminated entity reference T [CORE\Cake\Utility\Xml.php, line 292
... because (as documented), DOMDocument::createElement
does not escape values. However, it only does it in certain nodes, as the test case illustrates.
Am I doing something wrong or I just hit a bug in CakePHP?
The problem seems to be in nodes that have both attributes and values thus need to use the @
syntax:
'@id' => 'A & B: OK', // <-- Handled as plain text
'name' => 'C & D: OK', // <-- Handled as plain text
'@' => 'S & T: ERROR', // <-- Handled as raw XML
I've written a little helper function:
protected function escapeXmlValue($value){
return is_null($value) ? null : htmlspecialchars($value, ENT_XML1, 'UTF-8');
}
... and take care of calling it manually when I create the array:
'@id' => 'A & B: OK',
'name' => 'C & D: OK',
'@' => $this->escapeXmlValue('S & T: NOW WORKS FINE'),
It's hard to say if it's bug or feature since the documentation doesn't mention it.
这篇关于CakePHP Xml实用程序库触发DOMDocument警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!