i am curious to know how can i get the database data in xml format in Zend framework using context switching.
Do i need to compulsorily specify the format in my url, like:
http://localhost/pt/public/index.php/api/v1/users.xml?param1=3
I want to get the format from url (.xml, .json...) and apply the corresponding format to my output automatically.
Currently iam doing this: I get the user data from the database. I get the users marks based on the class id i pass to the url:
$id = $request->getParam('param1'); // get class id param
$users = new Application_Model_DbTable_Users();
$result = $users->fetchData($id);
if(count($result) != 0)
{
$doc = new DOMDocument();
$doc->formatOutput = true;
$root = $doc->createElement("Student");
$doc->appendChild($root);
foreach($result as $details)
{
$root_element = $doc->createElement("Marks");
$root->appendChild($root_element);
$TElement = $doc->createElement("Total");
$TElement->appendChild($doc->createTextNode($details->marks));
$root_element->appendChild($TElement);
}
$xml = $doc->saveXML();
$this->view->xml = $xml;
}
And in the corresponding view script, i have this code:
<?php
header('Content-type: text/xml');
echo $this->xml;
?>
I get the user data and use DOMDocument to write the xml output to the view. But is it possible to automatically generate the XML data from database, without using DOM ?