Deliver content as XML with Zend Framework

In the DevZone of ZF there’s a good tutorial: my aim is to give you a silly example of how to deliver content as XML in ZF and/or Magento.

Create a simple action in your controller:

1
2
3
4
5
6
<?php

public function xmlAction()
{
    ...
}

which we’re gonna use to construct a single-node ( sooo silly example ) XML structure.

First we create the DOMDocument:

1
2
3
<?php

$xml = new DOMDocument('1.0', 'utf-8');

then we create our nodes:

1
2
3
<?php

$xml->appendChild($xml->createElement('nodename', 'nodevalue'));

We close this routine operations telling that the output has to be saved as XML, but also that the headers might fit XML requirements:

1
2
3
4
<?php

$output = $xml->saveXML();
$this->_response->setHeader('Content-Type', 'text/xml; charset=utf-8')->setBody($output);

That’s it!


In the mood for some more reading?

...or check the archives.