代码之家  ›  专栏  ›  技术社区  ›  bradenkeith

用simplexml函数读取返回的xml

  •  0
  • bradenkeith  · 技术社区  · 15 年前

    这有点难以解释,所以提前谢谢你和我相处。

    我正在使用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 回复  |  直到 15 年前
        1
  •  3
  •   pleasedontbelong    15 年前

    使用simpleXML的构造函数的指令尝试使用 simplexml_load_string() 功能

        2
  •  0
  •   Steerpike    15 年前

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

    return echo $o;
    
    推荐文章