0

I have an array which needs to be converted into an XML view and show the same in a view page. The array looks something like this:

   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
    )

)

Let me tell you what I've done so far.

    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"

    }

Any help would be appreciated.

Peace! xD

3
  • If you use serialize why do you convert the data manually in the view? Cake should automatically convert the data. And are you sure the error comes from asXML() and not fromArray()? Commented Feb 4, 2016 at 8:37
  • Don't think there's any issue with the array part. And can you please guide me what to do after serializing? Commented Feb 4, 2016 at 8:47
  • book.cakephp.org/3.0/en/views/json-and-xml-views.html make sure you followed this. Commented Feb 4, 2016 at 10:37

1 Answer 1

1

AppController

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

routes.php

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

With these settings you're able to call your views with .xml If you're serializing the variables, which you are.

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

For more information about json and xml views in CakePHP 3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.