我正在尝试将cakephp请求对象中的所有表单数据转换为xml,然后将其转换为字符串,以便将其放置在mysql表的(blob)列中。
我目前正试图使用CakePHP2.x.x中的buildin-xml构建器来完成这项工作,如下所示,但遇到了一个错误。
if ($this->request->is('post')) {
$this->Survey->create();
$xml = Xml::build($this->request->data);
}
表格显示在下方
<?php echo $this->Form->create('Survey'); ?>
<fieldset>
<legend><?php echo __('Add Survey'); ?></legend>
<?php
echo $this->Form->input('Question 1');
echo $this->Form->input('Question 2');
echo $this->Form->input('Question 3');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
我得到的错误似乎是由于stacktrace中的DOCDocument->createElement(string,string)。我还使用了其他方法,包括像这样手动构建:
$doc = new DOMDocument('1.0');
$doc->formatOutput = true;
$doc->loadHTML($this->request->data);
$data = $this->request->input('Xml::build',
array('return' => 'domdocument'));
while(list($key,$value) = each($this->request->data)){
$data = $data . $key . $value;
}
if(isset($this->request->data)){
$doc = new DOMDocument('1.0');
$doc->formatOutput = true;
$root = $doc->appendChild($doc->createElement('survey'));
$post = $this->request->data['Survey'];
unset($post['submit']);
foreach($post as $key => $value){
$node = $doc->createElement($key,$value);
$root->appendChild($node);
}
$test1 = $doc->saveXML();
如有任何帮助,我们将不胜感激。非常感谢。
请参阅将数组转换为XML字符串部分。