2011-06-11 113 views
0

我需要将某些查询导出到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.''); 

    } 

回答

0

DomDocument::saveXML()犯规取文件的路径 - 它返回的XML文档的文本。如果您想直接将其保存为像您一样的文件,请使用DomDocument::save()

您的phtml中的所有逻辑都应该在您的控制器中。你也已经将上下文设置为xml,所以视图将输出XML到浏览器,而不是HTML ...所以id不显示确认消息,除非它使用XML编码,否则你需要使用默认上下文(HTML)。所以,如果你想将文件保存到网络服务器,并显示确认你会做:

public function exportXmlAction(){ 
    // remove the context switch from init 
    // do your doctrine stuff 
    // build the xml DOM as $xml 
    $xml->save('path/to/file'); 
    $this->message = 'File was exported successfully!'; 
} 

// in your phtml 
<?php echo $this->message; ?> 

如果你想在屏幕上显示的xml:

public function exportXmlAction(){ 
    // do your doctrine stuff 
    // build the xml DOM as $xml 
    $this->xmlDom = $xml; 
} 

// in your phtml 
<?php echo $this->xmlDom->saveXml(); ?> 

我有但现在的问题是whay用户是否希望将xml导出到服务器。当你打电话给DomDocument::save()时,你将该文件保存到服务器上的一个路径上,而不是用户的机器上,那么将xml导出到用户无法访问的服务器的目的是什么?

+0

感谢您的回复,我正在开发的应用程序将在Intranet上运行,以便管理员可以访问这些文件。我试过你的建议,但它似乎并没有为我工作,对不起,我从来没有使用过PHP和XML在创建XML文件。 – Graham 2011-06-11 19:15:07

+0

@Graham:你关掉了上下文切换吗?你确定文件保存好吗?你得到了什么错误? – prodigitalson 2011-06-11 19:23:32

+0

感谢您的回复,我关闭了上下文切换功能,通过将上面显示的代码移回到phtml文件中,出于某种原因,它在控制器中无法正常工作。 – Graham 2011-06-13 17:56:47