dougan4663 2011-03-04 06:26 采纳率: 0%
浏览 33
已采纳

Zend使用上下文切换以xml格式获取db数据

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 ?

  • 写回答

2条回答 默认 最新

  • douhan5547 2011-03-04 16:21
    关注

    emaillenin is right, there's nothing ZF can do to convert your data to XML.

    But instead of forming XML manually (with DOMDocument and the like), I suggest that you take a look at PEAR XML_Serializer package.

    XML_Serializer allows you to transform arrays, objects, etc. to well-formed XML. You can also specify your root name, default tag names, indentation type, etc. So, you're pretty much in control for the resulting XML.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao
  • ¥20 Vite 打包的 Vue3 组件库,图标无法显示
  • ¥15 php 同步电商平台多个店铺增量订单和订单状态
  • ¥15 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题