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!


Hi there! I recently wrote an ebook on web application security, currently sold on leanpub, the Amazon Kindle store and gumroad.

It contains 160+ pages of content dedicated to securing web applications and improving your security awareness when building web apps, with chapters ranging from explaining how to secure HTTP cookies with the right flags to understanding why it is important to consider joining a bug bounty program.

Feel free to skim through some of the free chapters published on this blog and, if the content seems interesting enough to you, grab a copy on leanpub, the Amazon Kindle store, gumroad or simply checkout right down below!

Buy the Web Application Security ebook for $6.99

In the mood for some more reading?

...or check the archives.