使用SimpleXML按函数读取返回的XML

时间:2010-08-18 15:11:33

标签: php function kohana simplexml

这有点难以解释,所以提前谢谢你对我的承诺。

我正在使用Kohana PHP框架来开发应用程序。我有一个接受搜索参数的模型函数,应该返回一个XML样式的页面。我需要控制器使用SimpleXML读取它。任何想法如何做到这一点?

    $o = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    $o .= "<feed>\n";
    $o .= "\t<search_phrase>$q</search_phrase>\n";

    if(isset($entries)){
        uasort($entries, 'compare_weight');

        /**
         * Build the xml data
         */

        foreach($modules as $module){
        $o .= "\t<search_location>$module</search_location>\n";
        }

        foreach($entries as $k=>$entry){
            $o .= "\n\t<entry>\n";
            $o .= "\t\t<title>$entry[title]</title>\n";
            $o .= "\t\t<url>$entry[url]</url>\n";
            $o .= "\t\t<weight>$entry[weight]</weight>\n";
            $o .= "\t\t<module>$entry[module]</module>\n";

            if($entry['owners']){
                foreach($entry['owners'] as $owner){
                    $o .= "\t\t<owners>\n";
                    $o .= "\t\t\t<owner_id>$owner[owner_id]</owner_id>\n";
                    $o .= "\t\t\t<owner_name>$owner[owner_name]</owner_name>\n";
                    $o .= "\t\t\t<profile_link>$owner[profile_link]</profile_link>\n";
                    $o .= "\t\t</owners>\n";
                }
            }

            $o .= "\t</entry>\n";
        }       
    }else{
        $o .= "\t<noresult>true</noresult>\n";
    }

    $o .= "</feed>\n";

    return $o;

控制器的功能如下......这是我能够最接近我的头脑的方法。

    $return= $this->search->search($_GET);

    $xml = new SimpleXMLElement($return);
    die($xml);

它返回一个包含44个空行的空白文档。任何方向都会有所帮助。

2 个答案:

答案 0 :(得分:3)

使用simplexml_load_string()函数

而不是使用simpleXML的构造函数

答案 1 :(得分:0)

尝试将$ xml Feed的返回值更改为:

return echo $o;