2011-06-11 64 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