我需要将某些查询导出到xml文件。我有这个工作,该文件被创建和数据导出,但我在屏幕上收到以下错误,因为该文件正在导出,而不是显示。Zend Framework将原则查询结果导出到XML文件
This page contains the following errors:
error on line 3 at column 1: Extra content at the end of the document
我承认,我是新来这是大多数人都知道,但有我可以导出,只是显示该报告已被保存的确认信息给用户,或者是一个办法我完全用错误的方式去解决这个问题?
我的代码如下
我控制器
public function init()
{
// allow certain reports to be exported to xml
// initialize context switch helper
$contextSwitch = $this->_helper->getHelper('contextSwitch');
$contextSwitch->addActionContext('newsletterexport', 'xml')
->initContext();
}
public function newsletterexportAction()
{
$q = Doctrine_Query::create()
->select('c.firstname,c.lastname,c.address1,c.address2,c.address3,t.county')
->from('PetManager_Model_Clients c')
->leftJoin('c.PetManager_Model_Counties t')
->where('c.consentToNews=1');
$result = $q->fetchArray();
if (count($result) >= 1) {
$this -> view -> records = $result;
}
}
编辑
好吧,我试图从xml.phtml代码移动到我的控制器的建议并试图用save保存文档,但现在我得到了xml文档的开始如下所示,但没有记录保存到文档中。
<?xml version="1.0" encoding="utf-8"?>
<petmanager:document xmlns:petmanager="http://petmanager"><petmanager:records/></petmanager:document>
我的控制器代码作为此次编辑
public function newsletterexportAction()
{
$q = Doctrine_Query::create()
->select('c.firstname,c.lastname,c.address1,c.address2,c.address3,t.county')
->from('PetManager_Model_Clients c')
->leftJoin('c.PetManager_Model_Counties t')
->where('c.consentToNews=1');
$result = $q->fetchArray();
if (count($result) >= 1) {
//$this -> view -> records = $result;
$docpref="newsletterexport";
$docDate=date('y-m-d');
$ext=".xml";
$docname=$docpref.$docDate.$ext;
// create XML document
$dom = new DOMDocument('1.0', 'utf-8');
// create root element
$root = $dom->createElementNS('http://petmanager','petmanager:document');
$dom->appendChild($root);
// convert to SimpleXML
$xml = simplexml_import_dom($dom);
// add resultset elements
$records = $xml->addChild('records');
foreach($this->$result as $r){
$record = $records->addChild('record');
$record->addChild('firstName',$this->escape($r['firstName']));
$record->addChild('lastName',$this->escape($r['lastName']));
$record->addChild('address1',$this->escape($r['address1']));
$record->addChild('address2',$this->escape($r['address2']));
$record->addChild('address3',$this->escape($r['address3']));
$record->addChild('county',$this->escape($r['PetManager_Model_Counties']['county']));
}//end of foreach
// saave document
$xml->saveXML();
$dom->save('D:/reports/exports/'.$docname.'');
}
感谢您的回复,我正在开发的应用程序将在Intranet上运行,以便管理员可以访问这些文件。我试过你的建议,但它似乎并没有为我工作,对不起,我从来没有使用过PHP和XML在创建XML文件。 – Graham 2011-06-11 19:15:07
@Graham:你关掉了上下文切换吗?你确定文件保存好吗?你得到了什么错误? – prodigitalson 2011-06-11 19:23:32
感谢您的回复,我关闭了上下文切换功能,通过将上面显示的代码移回到phtml文件中,出于某种原因,它在控制器中无法正常工作。 – Graham 2011-06-13 17:56:47