2016-02-04 62 views
0

我有一個數組需要轉換爲XML視圖並在視圖頁面中顯示相同的數組。該陣列看起來是這樣的:CakePHP 3將數組轉換爲XML

Array 
(
[0] => Cake\ORM\Entity Object 
    (
     [id] => 1 
     [user_id] => 3 
     [title] => Article 1 
     [body] => Validation is commonly used. 
     [created] => Cake\I18n\Time Object 
      (
       [time] => 2016-01-29T13:51:43+0000 
       [timezone] => UTC 
       [fixedNowTime] => 
      ) 

     [modified] => Cake\I18n\Time Object 
      (
       [time] => 2016-01-30T13:51:43+0000 
       [timezone] => UTC 
       [fixedNowTime] => 
      ) 

     [[new]] => 
     [[accessible]] => Array 
      (
       [*] => 1 
      ) 

     [[dirty]] => Array 
      (
      ) 

     [[original]] => Array 
      (
      ) 

     [[virtual]] => Array 
      (
      ) 

     [[errors]] => Array 
      (
      ) 

     [[repository]] => AppManager.Articles 
    ) 

[1] => Cake\ORM\Entity Object 
    (
     [id] => 2 
     [user_id] => 1 
     [title] => Article 2 
     [body] => The API documentation 
     [created] => Cake\I18n\Time Object 
      (
       [time] => 2016-01-03T13:51:58+0000 
       [timezone] => UTC 
       [fixedNowTime] => 
      ) 

     [modified] => Cake\I18n\Time Object 
      (
       [time] => 2016-01-03T13:51:58+0000 
       [timezone] => UTC 
       [fixedNowTime] => 
      ) 

     [[new]] => 
     [[accessible]] => Array 
      (
       [*] => 1 
      ) 

     [[dirty]] => Array 
      (
      ) 

     [[original]] => Array 
      (
      ) 

     [[virtual]] => Array 
      (
      ) 

     [[errors]] => Array 
      (
      ) 

     [[repository]] => AppManager.Articles 
    ) 

[2] => Cake\ORM\Entity Object 
    (
     [id] => 3 
     [user_id] => 2 
     [title] => Article 3 
     [body] => Now that you’ve created a validator and added the rules you want to it, you can start using it to validate data. Validators are able to validate array data. For example, if you wanted to validate a contact form before creating and sending an email you could do the following: 
     [created] => Cake\I18n\Time Object 
      (
       [time] => 2016-01-03T13:52:19+0000 
       [timezone] => UTC 
       [fixedNowTime] => 
      ) 

     [modified] => Cake\I18n\Time Object 
      (
       [time] => 2016-01-03T13:52:19+0000 
       [timezone] => UTC 
       [fixedNowTime] => 
      ) 

     [[new]] => 
     [[accessible]] => Array 
      (
       [*] => 1 
      ) 

     [[dirty]] => Array 
      (
      ) 

     [[original]] => Array 
      (
      ) 

     [[virtual]] => Array 
      (
      ) 

     [[errors]] => Array 
      (
      ) 

     [[repository]] => AppManager.Articles 
    ) 

[3] => Cake\ORM\Entity Object 
    (
     [id] => 8 
     [user_id] => 1 
     [title] => Sample webservices post 
     [body] => Get the cookies from the response. Cookies will be returned as an array with all the properties that were defined in the response header. To access the raw cookie data you can use header() 
     [created] => Cake\I18n\Time Object 
      (
       [time] => 2016-02-02T11:06:14+0000 
       [timezone] => UTC 
       [fixedNowTime] => 
      ) 

     [modified] => Cake\I18n\Time Object 
      (
       [time] => 2016-02-02T11:06:14+0000 
       [timezone] => UTC 
       [fixedNowTime] => 
      ) 

     [[new]] => 
     [[accessible]] => Array 
      (
       [*] => 1 
      ) 

     [[dirty]] => Array 
      (
      ) 

     [[original]] => Array 
      (
      ) 

     [[virtual]] => Array 
      (
      ) 

     [[errors]] => Array 
      (
      ) 

     [[repository]] => AppManager.Articles 
    ) 

讓我告訴你,我這樣做的遠。

public function getDataFromWebServices(){ 

     $articles = $this->Articles->find("all")->toArray(); 

     $this->set([ 
      "articles"  => $articles, 
      "_serialize" => ["articles"] 
     ]); 

     // Not sure what to do in the view after serializing this array 

     $xmlObject = Xml::fromArray($articles); 

     $xmlString = $xmlObject->asXML(); // Gives an error "Invalid Input" 

    } 

任何幫助,將不勝感激。

和平!的xD

+0

JSON和XML視圖更多信息,如果您使用的序列化你爲什麼在視圖中手動轉換數據? Cake應該自動轉換數據。你確定錯誤來自asXML()而不是fromArray()? – burzum

+0

不要認爲數組部分有任何問題。你能指導我在序列化後做些什麼嗎? –

+0

http://book.cakephp.org/3.0/en/views/json-and-xml-views.html請確保你遵循了這一點。 – burzum

回答

1

AppController的

public function initialize() 
{ 
    $this->loadComponent('RequestHandler'); 
} 

routes.php文件

$routes->extensions(['xml']); 

通過這些設置你能如果你用的.xml 打電話給你的意見序列化變量,你是。

/controller/get-data-from-web-services.xml

有關CakePHP的3

http://book.cakephp.org/3.0/en/views/json-and-xml-views.html